How to check if javascript cookie has ended?

Is it possible to check if javascript cookie ended with?

I need to do something conditionally, and two of these conditions overlap, for which, if it can be checked whether the cookie has expired, then it will be easier for me to complete all the actions.

I am using jquery-1.5.js and jquery.cookies.js .

Thanks.

CODE

 var jq = jQuery.noConflict(); jq(document).ready(function () { var timeStart, timeSubmit, timeLeft; timeSubmit = 5 * 60 * 1000; timeStart = jaaulde.utils.cookies.get("_watchman"); try { if(jaaulde.utils.cookies.test()) { throw "err1"; } else if(hasCookieExpired) { throw "err2"; } else if(!timeStart) { jaaulde.utils.cookies.set("_watchman", String(new Date().getTime()), {path: '/path', expiresAt: new Date((new Date().getTime() + timeSubmit))}); timeLeft = timeSubmit - (new Date().getTime() - Number(jaaulde.utils.cookies.get("_watchman"))); timeCheck(); } else { timeLeft = timeSubmit - (new Date().getTime() - Number(jaaulde.utils.cookies.get("_tts"))); timeCheck(); } } catch(err) { //handle errors } function timeCheck() { if(timeLeft <= 0) { triggerSubmit(); } else { setTimeout(triggerSubmit, timeLeft); setInterval(showTimeLeft, 1000); } } function triggerSubmit() { //submit it } function showTimeLeft() { //do something } }); 
+9
source share
4 answers
 if( $.cookie('yourCookie') === null ) { // EXPIRED } else { // DO SOMETHING ELSE } 

Or, like this, using the Ternary operator:

 $.cookie('yourCookie') === null ? /* EXPIRED */ : /* DO SOMETHING ELSE */; 

https://github.com/carhartl/jquery-cookie (no longer supported, archived)
https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
https://github.com/js-cookie/js-cookie

+10
source

The browser will automatically delete all cookies upon expiration, so all you have to do is check if the cookie exists or not.

+1
source

If you say that the browser did not delete the cookie, it is probably because your time zone is different from UTC.

+1
source

To check, enter the HTML below

 <label for="expsecs">Cookie expiration in seconds:</label> <input type="text" id="expsecs" value="5" /><br /> <button id="setcookie">set cookie</button> <button id="getcookie">get cookie</button> <div id="cookiestatus">status</div> 

Enter jQuery code below

 function set_cookie() { var secs = parseInt($('#expsecs').val(), 10); if (isNaN(secs)) { secs = 5; } var now = new Date(); var exp = new Date(now.getTime() + secs*1000); var status = ''; document.cookie = 'ExpirationCookieTest=1; expires='+exp.toUTCString(); if (document.cookie && document.cookie.indexOf('ExpirationCookieTest=1') != -1) { status = 'Cookie successfully set. Expiration in '+secs+' seconds'; } else { status = 'Cookie NOT set. Please make sure your browser is accepting cookies'; } $('#cookiestatus').text(status); } function get_cookie() { var status = ''; if (document.cookie && document.cookie.indexOf('ExpirationCookieTest=1') != -1) { status = 'Cookie is present'; } else { status = 'Cookie is NOT present. It may be expired, or never set'; } $('#cookiestatus').text(status); } function init() { $('#setcookie').bind('click', set_cookie); $('#getcookie').bind('click', get_cookie); } $(init); 

Check it out in jsfiddle

+1
source

All Articles