Thursday, November 6, 2008

Browser Close Vs Session End in

Browser Close Vs Session End in

After a few R&D’s here goes my findings

There are n number of scenarios by which the browser can be closed viz

1. Clicking the (x) button
2. Clicking Alt+F4
3. File menu à Exit.
4. Alt+f+x

The OnUnload event will fire for all the above scenarios. So write a Javascript function like this which uses Remote scripting to Abandon the session in the Server side.

function abandonSession()
// Use remote scripting to
var xmlhttp;
if (window.ActiveXObject)
{ // IE
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");"GET","AbandonSession.aspx",false);

Attach the above function in the HTML as shown below

In the page load of AbandonSession.aspx

protected void Page_Load(object sender, EventArgs e)
// Write your code to logout the user since sometimes Session_End may not fire. Also Session_Event in Global.asax will not fire in Web farms.

However the weird thing is the OnUnload event also fires during following scenarios

1. When F5 or Page refresh is done
2. When the user types a different url in the same browser
3. Page is submitted or Post back happens
4. Click the browser back

So somehow you need to figure out a way which will not execute the abandonSession() function for all the above scenarios.

No comments: