Click here to Skip to main content
11,429,382 members (66,931 online)
Click here to Skip to main content
Alternative Tip/Trick

How to end user session when browser closed

, 8 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
//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) ASTI Transportation Inc.
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...

v//
Follow on   Twitter   Google+

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
lleyva15-Feb-11 7:26
memberlleyva15-Feb-11 7:26 
GeneralPlease edit your tip to wrap the code in a pre block Pin
DaveAuld8-Feb-11 14:38
mentorDaveAuld8-Feb-11 14:38 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150428.2 | Last Updated 9 Feb 2011
Article Copyright 2011 by jfriedman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid