How can I get the reason for unloading a page in a javascript onunload event, in IE?

There can be various reasons for unloading the page:  
1 The user closes the current window.  
2 The user moves to another location.  
3 Press the Back, Forward, Refresh, or Home button.  
4 The user submits the form, and then the browser starts to unload the current page and load the page with the results of the form submission. (Assuming the current window is the target of the form).  
5 and so on ...

Can I somehow find out in the onunload handler that the reason for unloading is p.4, i.e. Go to the form submission results page?
I can define some flag when submitting the form, but that doesn't solve the problem. Since the response (in the form submit) from the web server takes some time, the browser does not unload the current page immediately and expects a response from the server. And during this wait, the user can close the window or move anywhere. And I need to know if it actually went to the results page or something else ...?

+2


a source to share


2 answers


You can capture some of these events.



For example, for links, you can add a link event handler that saves the attribute href

, does what you want, and then sets window.location

to href

which you stored in a variable.

+1


a source


The exact cause of the page unload cannot be known in the unload handler. The OnUnload event is non-standard and was first implemented by IE.

Different browsers may handle it differently and fire the event for different cases.

msdn link



mozilla link

So, if you are trying to find out the reason for the unloading in the unloading handler, I think you might be out of luck. However, as Alex pointed out in his answer, you probably know that a user can navigate from your page by clicking a link on their page, making your click handlers for those links smarter.

0


a source







All Articles