Tuesday, August 30, 2011

Before unloading

How to perform some request before closing the browser. Right in JavaScript:

window.onbeforeunload = closeAction;

function closeAction()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.open("GET",your_own_url_here,false);
xmlhttp.onreadystatechange=function()
{

}
xmlhttp.send();
}

We are using this in Chat servlet for example. Automatically logout for users closed the browser.

No comments: