Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reliability of onbeforeunload

How reliable is the event window.onbeforeunload?

  1. Does it fire in all major browsers?
  2. Does it fire if the client browser crashes?
  3. Is it able to delay the close event in case it might need "a little longer" or will it get cut off?
  4. Are there any alternatives?
like image 625
RienNeVaPlu͢s Avatar asked May 30 '13 15:05

RienNeVaPlu͢s


People also ask

What triggers Onbeforeunload?

The onbeforeunload event occurs when the document is about to be unloaded. This event allows you to display a message in a confirmation dialog box to inform the user whether he/she wants to stay or leave the current page. The default message that appears in the confirmation box, is different in different browsers.

What does window Onbeforeunload do?

The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point. This event enables a web page to trigger a confirmation dialog asking the user if they really want to leave the page.

How do I stop Onbeforeunload?

$(window). bind('beforeunload',function() { return "'Are you sure you want to leave the page. All data will be lost!"; }); $('#a_exit').

What is the difference between unload and Beforeunload?

beforeunload event – the user is leaving: we can check if the user saved the changes and ask them whether they really want to leave. unload – the user almost left, but we still can initiate some operations, such as sending out statistics.


1 Answers

  1. Does it fire in all major browsers?
    Yes, onbeforeunload works in every browser I have ever tried it on.

  2. Does it fire if the client browser crashes?
    No, if a browser crashes then it dies, there is no way around that fact.

  3. Is it able to delay the close event in case it might need "a little longer" or will it get cut off?
    I said "no" in my comment, but it does depend somewhat. For instance, if you have a really long loop, it will fully execute before the page closes. However, you cannot use AJAX or any other asynchronous function like setTimeout within the handler. It MUST return a string in a synchronous manner in order to block unloading.

  4. Are there any alternatives?
    Again, I said "no" in my comment, but I can elaborate further. If you attach an event handler to your page to detect a click on a link, you can then manually call the onbeforeunload handler to get the string response, and use that to show a custom confirmation box, cancelling the click event as you do so. Note that this will not affect reloading, Back, Forward or Close buttons, only clicking on a link to a new page. You should also take care to not affect links to anchors on the same page, or javascript:void(null);-type links. This will allow you to make AJAX requests too, and if the user confirms wanting to leave the page you can do some things before loading the new page.

    Long story short, there are alternatives in some cases, but it all stems from the onbeforeunload event and can be quite complex.

like image 52
Niet the Dark Absol Avatar answered Oct 21 '22 06:10

Niet the Dark Absol