Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
48
rated 0 times [  50] [ 2]  / answers: 1 / hits: 33693  / 12 Years ago, fri, december 14, 2012, 12:00:00

i need to delete cookie when browser is closed and already put on window.onbeforeunload along with other as following:



 window.onbeforeunload = function(){
location.replace(admin.jsp?action=logout);
deleteCookie('Times');
}


with setCookie as following:



 function setCookie(name,value,days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = ; expires=+date.toGMTString();
}
else {
expires = ;
}
document.cookie = name+=+value+expires+; path=/;
}


when it comes to deleteCookie, it contains as below:



    setCookie(name,value,-1);


The problem is whenever i restart browser, it always comes to window.onbeforeunload so that deleteCookie fired. I actually need this to reset my countdown timer whenever user logs in since the cookie never deleted if the counter doesn't end and user sometimes closes window/tab before it ends. So, my idea is either reset the counter when user logs in or simply delete cookie when user logs out. I still can't figure out how to code this, however. Can anyone help me out? Code snippet will be appreciated, though. CMIIW


More From » javascript

 Answers
11

Not specifying Expires value on the cookie will cause the cookie to be removed when the browser session ends i.e. when user closes the browser. Like:



Set-Cookie: made_write_conn=1295214458; Path=/; Domain=.foo.com


The made_write_conn cookie made_write_conn does not have an expiration date, making it a session cookie. It will be deleted after the user closes his/her browser.
Try doing:



setCookie('Times',value, '');

[#81431] Wednesday, December 12, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brandt

Total Points: 43
Total Questions: 90
Total Answers: 111

Location: Aruba
Member since Fri, Jun 24, 2022
2 Years ago
;