Click here to Skip to main content
Click here to Skip to main content
Alternative Tip
Go to top

How to end user session when browser closed

, 8 Feb 2011
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...
Follow on   Twitter   Google+

Comments and Discussions

Generalhey everyone I am trying to find a way to catch browser clos... Pinmember17sam30-Nov-11 0:45 
General0 down vote favorite share [fb] share [tw] I am trying... Pinmember17sam30-Nov-11 0:44 
GeneralReason for my vote of 4 It doesnt really end session, it jus... PinmemberHaBiX15-Nov-11 0:14 
GeneralReason for my vote of 5 excellent additional information Pinmemberlleyva15-Feb-11 6:26 
GeneralPlease edit your tip to wrap the code in a pre block PinmentorDaveAuld8-Feb-11 13: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 | Mobile
Web02 | 2.8.140916.1 | Last Updated 9 Feb 2011
Article Copyright 2011 by jfriedman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid