Retrieve cookie expiration time in javascript or jquery

Asked

Viewed 1,312 times

3

Is there any way to recover the remaining time for expiration of a javascript or jquery cookie?

  • When you write "recover" you mean know the time you have? or change (set again)?

  • I want to know how much longer to expire.

  • Have you tried it $.cookie('nome_do_cookie');?

  • Yes and this does not bring a cookie object just the value of it.

2 answers

5


You simply cannot get this kind of information with Javascript because it is not available at document.cookie.

However, as there is a common whim for everything, if you are the person setting this cookie, you can create a second to serve as a support, storing in it the creation dates.

  • I researched here too and that’s really it. Thank you for the answer

  • I wasn’t sure about that either. I spent about 40 minutes researching and testing alternative solutions suggested in Soen, confirming in several other sites, blogs, mailing lists etc. until arriving at a definitive opinion, including with a wrong but viable alternative. I just wish I could have brought something from an RFC, but I couldn’t find anything in the text that specifically said that.

5

You already have an accepted answer and in fact it is correct because you can not read this information.

I leave one more reply with an idea in case the Cookie is created by you. You can write the end date in the Cookie itself.

var data = new Date();
data.setSeconds(data.getSeconds() + 10); // Cookie com 10 segundos de vida
data = new Date(data);

$.cookie('meu_cookie', data, {
    expires: data
});

Example: http://jsfiddle.net/6pQ5T/

Note that Cookies can be changed by the user and are therefore not to be trusted.

  • 1

    Very good answer too. I know this cookie vulnerability but I also measured the risk, because the key and value of the cookie are encrypted in Ceaser. Any change causes it to fall into the false block of if.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.