年龄验证弹出:设置cookie以记住决策

时间:2017-10-01 08:41:56

标签: javascript jquery html css remember-me

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

$(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>
&#13;
&#13;
&#13;

(代码来源:jsfiddle.net

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

1 个答案:

答案 0 :(得分:1)

以下实施使用cookie来存储和检索年龄协议的状态。您应该在JavaScript中添加一个条件,以在应用程序启动时识别协议状态。同样,默认使用popup隐藏css

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使用 Cookie

$(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();
  });

});

使用 localStorage

的JavaScript
// 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();
  });
});