简体   繁体   中英

Set cookie for every page

I'm trying to implement a pop up that only shows when a cookie isn't present. It all works fine apart from one thing that in another part of the website the pop up still shows up and the user has to confirm again to close it. I've tried to set a path but I must be doing something wrong. This is the code :

<script type="text/javascript">
 function dontshow() {
    var date = new Date();
    date.setTime(date.getTime() + (3600 * 1000 * 24 * 365 * 10));
    $.cookie('visited', 'yes', '/', { expires: date }); // Set the cookie.
    $('#cookie_box').animate({ opacity: 0 }, "slow");
    $('#ContentMargin').removeClass('marginPopUp');
  }

</script>

Any suggestions what I'm doing wrong?

It is obvious that either

cookie is not being created even though code is written for that. or cookie checking code is broken and it cannot decide whether cookie is exist or not.

Please make sure checking two points above and you should be good.

$.cookie('visited', 'yes', '/', { expires: date });

according to docs path should be set in the same object where you set expires like so:

$.cookie('name', 'value', { expires: 7, path: '/' });

https://github.com/carhartl/jquery-cookie#readme

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM