Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does a jQuery cookie delete itself when it expires?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading