Intimate Closing the window
In many situations , people need to find out whether the user has closed the window or is out of that page.... For that to find out we need to tap into the window.onunload event. make an synchronous call to the server to intimate. this can also be used to post the data in the form. You cant make a "async" or "ajax"
call because the thread wont wait for the request to be completed(async) and the window will be closed before the request gets over.
window.onunload=function ()
{
//req ---> XMLHTTPRequest object
req.open("POST",URL,false/*synchronous call*/);
req.setRequestHeader("Cache-Control","no-cache");
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.send(post-items);
};
but there is a problem here , whenever the user navigates from this page or refreshes the page, the server will get the intimation. Now the problem here is howto distinguish between navigation out of the page and the closing of window. One way to do it would be to have a flag which will be set whenever the user clicks that link.
it will look like this
<a href=URL onclick="navigate-flag = 1;">
This flag can be checked in the onunload event handler.
| sun | pvs
0 comments:
Post a Comment