Crossbrowser Onbeforeunload

How to make a cross-browser on-window-unload request?

The short answer is that you can't.

The onbeforeunload event was initially introduced by Microsoft to allow a standard confirmation dialog. It is now supported in the original form by most browsers and in most case a short, non interactive, function is allowed to execute (for example you may log your state in the localStorage). But, as described in the cross-browser jQuery API, this is unreliable :

The exact handling of the unload event has varied from version to
version of browsers. For example, some versions of Firefox trigger the
event when a link is followed, but not when the window is closed. In
practical usage, behavior should be tested on all supported browsers,
and contrasted with the proprietary beforeunload event.

As there are many potential security problems related to the execution of a task when the user asked to end the page (and thus the script), this will probably be unreliable until (and if) a serious normalization effort is done to precise what exactly can be done in a onbeforeunload callback.

For the predictable future, it's recommended to not depend on onbeforeunload but to use other schemes, for example constant background saving or a big "save" button.

cross-browser Onunload and Onbeforeunload ? (particularly opera 11)

Opera does not yet support onbeforeunload. Opera's onunload support is incomplete - for example, it does not fire when you close a tab, or close Opera. As far as I know, while you can use onunload to show a warning if a user clicks a link to navigate away from a page with an unsaved form, there is no way you can prevent the navigation from happening when the unload event fires.

What I would do here is to listen for the onbeforeunload event, make it work in other browsers and wait for Opera to catch up. Whether it will be in Opera 12 or 13 or 15 I don't know, but when onbeforeunload is implemented it will work like it does in other browsers already.



Related Topics



Leave a reply



Submit