Click here to Skip to main content
15,860,859 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

How to end user session when browser closed

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
8 Feb 2011CPOL 15.6K   2   5
//Using MooToolswindow.addEvent('unload', Cookie.delete.pass('applicationCookie'));//using JavaScript by itself...//http://www.quirksmode.org/js/cookies.htmlvar eraser = function () { eraseCookie('applicationCookie'); };if(window.addEventListener) window.addEventListener('unload',...
//Using MooTools
window.addEvent('unload', Cookie.delete.pass('applicationCookie'));


//using JavaScript by itself...
//http://www.quirksmode.org/js/cookies.html
var eraser = function () { eraseCookie('applicationCookie'); };
if(window.addEventListener) window.addEventListener('unload', eraser, false); 
else if(window.attachEvent) window.attachEvent('onunload', eraser);


Your method removes all other events from unload of the body and is bad practice. In this example, I delete a cookie which is obviously something which can be done even easier but then the same can be said for your article.

You could make an XMLHttpRequest instead of deleting a cookie and this would be the way to go.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Livin in a lonely world, caught the midnight train going anywhere... Only thing is it was a runaway train... and it ain't ever goin back...
мала ка на хари, Trahentes ex exsilium

Comments and Discussions

 
Generalhey everyone I am trying to find a way to catch browser clos... Pin
17sam30-Nov-11 0:45
17sam30-Nov-11 0:45 
General0 down vote favorite share [fb] share [tw] I am trying... Pin
17sam30-Nov-11 0:44
17sam30-Nov-11 0:44 
GeneralReason for my vote of 4 It doesnt really end session, it jus... Pin
HaBiX15-Nov-11 0:14
HaBiX15-Nov-11 0:14 
GeneralReason for my vote of 5 excellent additional information Pin
Luis Leyva15-Feb-11 6:26
Luis Leyva15-Feb-11 6:26 
Reason for my vote of 5
excellent additional information
GeneralPlease edit your tip to wrap the code in a pre block Pin
DaveAuld8-Feb-11 13:38
professionalDaveAuld8-Feb-11 13:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.