Click here to Skip to main content
15,118,508 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick
Posted 8 Feb 2011

Stats

14.2K views
2 bookmarked

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
//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)

Share

About the Author

jfriedman
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 1:45
Member17sam30-Nov-11 1:45 
General0 down vote favorite share [fb] share [tw] I am trying... Pin
17sam30-Nov-11 1:44
Member17sam30-Nov-11 1:44 
GeneralReason for my vote of 4 It doesnt really end session, it jus... Pin
HaBiX15-Nov-11 1:14
MemberHaBiX15-Nov-11 1:14 
GeneralReason for my vote of 5 excellent additional information Pin
Luis Leyva15-Feb-11 7:26
MemberLuis Leyva15-Feb-11 7:26 
GeneralPlease edit your tip to wrap the code in a pre block Pin
DaveAuld8-Feb-11 14:38
professionalDaveAuld8-Feb-11 14: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.