年龄验证弹出窗口:设置 cookie 以记住决定

Age Verification Popup: Set cookie to remember decision

我正在使用以下代码在香烟网站上显示年龄验证弹出窗口:

$(function() {
  $('[data-popup-close]').on('click', function(e) {
    var targeted_popup_class = jQuery(this).attr('data-popup-close');
    $('[data-popup="' + targeted_popup_class + '"]').fadeOut(350);
    e.preventDefault();
  });
});

// Popup Age Verification
$(function() {
  $('[data-popup-close]').on('click', function(e) {
    var targeted_popup_class = jQuery(this).attr('data-popup-close');
    $('[data-popup="' + targeted_popup_class + '"]').fadeOut(350);
    e.preventDefault();
  });
});
#popup {
    z-index: 1000;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    position: fixed;
    background-color: rgba(0, 0, 0, 0.8);
}

.verify-window {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 300px;
    height: 200px;
    margin-left: -150px;
    margin-top: -100px;
    overflow: hidden;
    border-radius: 10px;
    padding: 20px;
    background-color: #fff;
    box-sizing: border-box;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
}

.verify-window img {
    display: block;
    width: 250px;
    height: 50px;
    margin: 0 auto;
    padding: 20px;
}

.verify-window h3 {
    font-family: 'Roboto', sans-serif;
    font-size: 1em;
    color: #4d4d4d;
    line-height: 1.7;
    margin-top: 10px;
    text-align: center;
}

.verify-window p {
    font-family: 'Roboto', sans-serif;
    font-size: 1em;
    color: #4d4d4d;
    margin-top: 10px;
    line-height: 1.7;
    text-align: center;
}

.button-yes,
.button-no {
    background: #fff;
    color: #ADCC21;
    width: 20%;
    margin-top: 10px;
    border: 2px solid #ADCC21;
    padding: 12px 17px;
    border-radius: 30px;
    font-family: 'Roboto', sans-serif;
    text-align: center;
    font-size: 1em;
}

.button-no {
    float: right;
    margin-right: 20px;
    border: 2px solid #95969a;
    color: #95969a;
    display: block;
}

.button-yes {
    float: left;
    margin-left: 20px;
}

.button-yes:hover {
    background: #ADCC21;
    color: #fff;
    transition: all 0.2s ease;
    cursor: pointer;
}

.button-no:hover {
    background: #95969a;
    color: #fff;
    transition: all 0.2s ease;
    cursor: pointer;
}
<div id="popup" data-popup="popup-1">
    <div class="verify-window">
        <h3>Age Verification</h3>
        <p>Are you at least 18 years old?</p>

        <div class="button-yes" data-popup-close="popup-1">
            Yes
        </div>

        <a href="https://www.google.com" target="_parent">
            <div class="button-no">
                No
            </div>
        </a>
    </div><!-- // verify window -->
</div>

<div id="content">
    <p>Page Content</p>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

(代码来源:jsfiddle.net

我想实现一个 cookie 来记住用户何时单击 .button-yes 按钮。另一个按钮不应该有这样的功能。我怎样才能最好地实现它?

以下实现使用 cookie 来存储和检索年龄协议的状态。您应该在 JavaScript 中添加一个条件来标识应用程序启动时的协议状态。还默认使用 css.

隐藏 popup

CSS

#popup {
  z-index: 1000;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  position: fixed;
  display: none;
  background-color: rgba(0, 0, 0, 0.8);
}

JavaScript 使用 Cookies

$(function() {

  //Check it the user has been accpeted the agreement
  if (!(document.cookie && document.cookie == "accepted")) {
    $("#popup").show();
  }

  $('[data-popup-close]').on('click', function(e) {
    var targeted_popup_class = jQuery(this).attr('data-popup-close');
    $('[data-popup="' + targeted_popup_class + '"]').fadeOut(350);

    //Set a cookie to remember the state
    document.cookie = "accepted";

    e.preventDefault();
  });

});

JavaScript 使用 localStorage

// Popup Age Verification

$(function() {

  //Check it the user has been accpeted the agreement
  if (!localStorage.getItem('accepted')) {
    $("#popup").show();
  }

  $('[data-popup-close]').on('click', function(e) {
    var targeted_popup_class = jQuery(this).attr('data-popup-close');
    $('[data-popup="' + targeted_popup_class + '"]').fadeOut(350);

    //Set a cookie to remember the state
    localStorage.setItem('accepted', true);

    e.preventDefault();
  });
});