Click here to Skip to main content
12,949,601 members (67,997 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 8 Feb 2011

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...// eraser = function () { eraseCookie('applicationCookie'); };if(window.addEventListener) window.addEventListener('unload',...
//Using MooTools
window.addEvent('unload', Cookie.delete.pass('applicationCookie'));

//using JavaScript by itself...
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.


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


About the Author

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...


You may also be interested in...


Comments and Discussions

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170524.1 | Last Updated 9 Feb 2011
Article Copyright 2011 by jfriedman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid