Link to home
Start Free TrialLog in
Avatar of Jon Imms
Jon ImmsFlag for United States of America

asked on

Create a cookie onclick

How can i modify this function, so it creates a cookie called bt-test which expires in 1 year?


$(document).ready(function(){
  $('#wt-cli-accept-all-btn, #wt-cli-reject-btn').on('click', function() {      $('html.has-not-scrolled #desktop-header, html.has-not-scrolled #mobile-header, html.has-not-scrolled main').animate({top: '0px'});   });

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I suggest you use localStorage

Something like this

const aYear = 365 * 24 * 60 * 60 * 1000; // close enough
$(function(){
  $('#wt-cli-accept-all-btn, #wt-cli-reject-btn').on('click', function() {
    const now = new Date();
    const bt-test = localStorage.getItem("bt-test");
    const expiry = new Date(bt-test || now.getTime());
    const diff = now.getTime() - expiry.getTime()
    if (!bt-test || diff > aYear) {
      $('html.has-not-scrolled #desktop-header, html.has-not-scrolled #mobile-header, html.has-not-scrolled main').animate({top: '0px'});
      localStorage.setItem("bt-test",now.getTime()+aYear);
    }
  });
});

Open in new window