Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
107
rated 0 times [  109] [ 2]  / answers: 1 / hits: 43532  / 13 Years ago, mon, january 30, 2012, 12:00:00

Is it possible to test if a javascript cookie has expired using?



I need to do a few thing conditionally and two of those conditions are overlapping for which if it could be tested whether a cookie has expired then it will be easier for me to get things done.



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



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
}

});

More From » jquery

 Answers
14
if( $.cookie('yourCookie') === null ) {
// EXPIRED
} else {
// DO SOMETHING ELSE
}


Or like this using Ternary operator:



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





https://github.com/carhartl/jquery-cookie (No longer maintained, archived)

https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie

https://github.com/js-cookie/js-cookie


[#87722] Sunday, January 29, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
clarissakourtneyb

Total Points: 710
Total Questions: 89
Total Answers: 125

Location: Dominica
Member since Sat, Nov 5, 2022
2 Years ago
;