Home > Enterprise >  Does a jQuery cookie delete itself when it expires?
Does a jQuery cookie delete itself when it expires?

Time:11-06

I'm using jQuery Cookie and was wondering what happens when a cookie expires. Does it delete itself or do I have to delete it myself?

I'm setting my cookie like so

    if ($.cookie('myCookie') == undefined) {
        $.cookie('myCookie', 'false', { expires: 1 });
    }

If it does delete itself then I just want to check if it exists or not so I can make another one.

If I do need to delete it, how can I check if it's expired?

CodePudding user response:

Yes, If a cookie has expired, the browser does not send that particular cookie to the server with the page request; instead, the expired cookie is deleted. So when a cookie is expired it is completely deleted. You ask how to check if a cookie is expired but you really just need to check if it exists. Here is an example of how:

if (typeof $.cookie('cookie_name') === 'undefined'){
 //Cookie Expired
} else {
 //Valid Cookie
}
  • Related