Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I override the OnBeforeUnload dialog and replace it with my own?

I need to warn users about unsaved changes before they leave a page (a pretty common problem).

window.onbeforeunload = handler 

This works but it raises a default dialog with an irritating standard message that wraps my own text. I need to either completely replace the standard message, so my text is clear, or (even better) replace the entire dialog with a modal dialog using jQuery.

So far I have failed and I haven't found anyone else who seems to have an answer. Is it even possible?

Javascript in my page:

<script type="text/javascript">        window.onbeforeunload = closeIt; </script> 

The closeIt() function:

function closeIt() {   if (changes == "true" || files == "true")   {       return "Here you can append a custom message to the default dialog.";   } } 

Using jQuery and jqModal I have tried this kind of thing (using a custom confirm dialog):

$(window).beforeunload(function () {     confirm('new message: ' + this.href + ' !', this.href);     return false; }); 

which also doesn't work - I cannot seem to bind to the beforeunload event.

like image 862
flesh Avatar asked Nov 09 '08 23:11

flesh


People also ask

What is the difference between Onbeforeunload and Onunload?

One significant difference (other than cancelability) between the onbeforeunload and onunload is that the former is triggered for download links and the latter is not.

How do I cancel Onbeforeunload?

Cancelable: The beforeunload event can be canceled by user interaction: // by https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload#Example window. addEventListener("beforeunload", function(event) { event. preventDefault(); // Cancel the event as stated by the standard.

What triggers Onbeforeunload?

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.

Is it possible to display a custom message in the Beforeunload popup?

7 Answers. Show activity on this post. A quick note (since this is an old answer) - these days all major browsers don't support custom message in the beforeunload popup. There is no new way to do this.


1 Answers

You can't modify the default dialogue for onbeforeunload, so your best bet may be to work with it.

window.onbeforeunload = function() {     return 'You have unsaved changes!'; } 

Here's a reference to this from Microsoft:

When a string is assigned to the returnValue property of window.event, a dialog box appears that gives users the option to stay on the current page and retain the string that was assigned to it. The default statement that appears in the dialog box, "Are you sure you want to navigate away from this page? ... Press OK to continue, or Cancel to stay on the current page.", cannot be removed or altered.

The problem seems to be:

  1. When onbeforeunload is called, it will take the return value of the handler as window.event.returnValue.
  2. It will then parse the return value as a string (unless it is null).
  3. Since false is parsed as a string, the dialogue box will fire, which will then pass an appropriate true/false.

The result is, there doesn't seem to be a way of assigning false to onbeforeunload to prevent it from the default dialogue.

Additional notes on jQuery:

  • Setting the event in jQuery may be problematic, as that allows other onbeforeunload events to occur as well. If you wish only for your unload event to occur I'd stick to plain ol' JavaScript for it.
  • jQuery doesn't have a shortcut for onbeforeunload so you'd have to use the generic bind syntax.

    $(window).bind('beforeunload', function() {} ); 

Edit 09/04/2018: custom messages in onbeforeunload dialogs are deprecated since chrome-51 (cf: release note)

like image 167
Owen Avatar answered Sep 17 '22 00:09

Owen