Search code examples
javascriptjqueryhtmlcssremember-me

Age Verification Popup: Set cookie to remember decision


I am using the following code to display an age verification popup on a cigarette website:

$(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>

(Code source: jsfiddle.net)

I want to implement a cookie that remembers when the user clicks on the .button-yes button. The other button shouldn't have a feature like this. How can I realize it the best way?


Solution

  • Following implementation uses a cookie to store and retrieve the state of the age agreement. You should add a condition in JavaScript to identify the agreement state when the application starts. Also hide the popup by default using 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 using 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 using 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();
      });
    });