Retrieve cookie expiration time in javascript or jquery

3

Is there any way to retrieve the time remaining for a cookie to expire in javascript or jquery?

    
asked by anonymous 28.07.2014 / 23:37

2 answers

5

You simply can not get this kind of information with JavaScript because it is not available in document.cookie .

However, since there is a problem for everything, if you are setting this cookie, you can create a second to support it, storing the creation dates in it.

    
29.07.2014 / 02:42
5

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

I leave one more response with an idea in case Cookie is created by you. You can write the end date in 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: link

Note that Cookies can be changed by the user, so they can not be trusted.

    
29.07.2014 / 09:47