I have implemented some function when the browser will be closed.
window.onbeforeunload = function (evt) {
evt = (evt)? evt:event;
clickY = evt.clie
The guy in this thread says that his approach based on the onbeforeunload
event works in IE.
IMHO, however, your best (and cross-platform) chance to do what you want is to setup a Ajax polling request (here is a nice demo) to check how much time has passed since the last user activity and clear the cache accordingly.
window.onbeforeunload event is calls before your page is unloaded. There is no method to check if it's refresh or browser window close event.
It fires by different scenarios MSDN:
anchor.click
method.document.write
method.document.close
method.window.close
method.window.navigate
or NavigateAndFind method.location.replace
method.location.reload
method.location.href
property.type=submit
control, or invoke the form.submit
method.window.open
method, providing the possible value _self
for the window name.document.open
method.And you can't handle this.
Also note that window.onbeforeunload
event supposed to inform user that he leaves page and handler function should return
string value that will be included in confirmation popup (except Firefox 4+
see bug).
You can not force the user to stay on the page.
Note: The only thing you can do with onbeforeunload
is to notify the user that he is leaving your page
(this is because some evil pages can open another pages and so on... and spam users. This is security restriction).
If you want to make AJAX call before window is unloaded you need to use onunload
event. Note that your AJAX call must me synchronus. Asynchronous call will not work.
I worked on this same problem for awhile so I will explain to you what I found out.
1) the window.onbeforeunload
event is handled by each browser a little differently - i've seen the session get lost as soon as the event fires regardless if the user chooses to stay on the page or not.
2) a web application simply doesn't have the power to prevent a user from closing the browser or navigating away from a page (for obvious reasons) but by implementing a server side hook I was able to call the necessary clean up code only when the browser was closed so I didn't bother prompting the user (which is kind of like hijacking their browser anyway)
If you need to prompt the user before closing the window then you are going to prompt them when they type in a new url as well. They are one and the same