简体   繁体   中英

Setting cookie expiry time using javascript

I have some javascript on my website that loads a modal on a specific page. It uses a cookie to determine whether someone has seen the modal. I need to set an expiry date/time for this but don't know how. I've checked on here for answers but can't see anything that helps me with the code I've got.

Can anyone help? I thought the '365' in the code specified the no. of days for the cookie to expire but it's expiring when the browser session ends.

<script type="text/javascript">
 $(document).ready(function() {
     if ($.cookie('MyCIEH_popup') == null) {
         $('#modalLarge').modal('show');
         $.cookie('MyCIEH_popup', '365;');
     }
 });
</script>

replace $.cookie('MyCIEH_popup', '365;'); with:

$.cookie('MyCIEH_popup', 'value', { expires: 365 });

Note: It's probably a better solution to use native js for that, see here .

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