views:

23131

answers:

6

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.

+44  A: 

well it looks like 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 short cut for onbeforeunload so you'd have to use the generic bind syntax.

    $(window).bind('beforeunload', function() {} );
    
Owen
I was using OnBeforeUnload in order to return the user to the page, rather than actually allowing them to leave - onunload occurs after the page has unloaded surely?Also, I cannot get your onunload function to work for some reason?
flesh
oh ugh you're right, i'll edit my post to reflect the latest info i found
Owen
I've found that the JQuery example of given at the end not to work in firefox. Stick with the simple way instead: window.onbeforeunload = function() {...}
jb
I've gone with jb's solution, not least because the return value is significant; also, for newcomers to this, note that 'return;' in your event handler will make sure nothing pops up at all if you don't actually have any outstanding work to do or you can resolve it without talking to the user.
ijw
Adding this comment mostly so google can relate mootools or other JS libraries with the post. It seems you have to use the straight JS method for this regardless of what library you are using. It may be possible, but not worth the hassle.
tj111
+9  A: 

Owen is correct. And, the reason behind this is security. Preventing a page from unloading is useful in web forms and such, but it can easily be exploited by a malicious site to fool the user into staying on a page. That's why web browsers implement the standard message and have this mechanism for inserting custom text.

pluckyglen
+2  A: 

Try this:

$(window).unload(function()
{
    alert('blubb'):
});

It works - no problems with jQuery.

Manu
The unload event occurs to late to prevent the loss of unsaved data.
kingjeffrey
A: 

see here for more info: http://stackoverflow.com/questions/140460

Ben McIntyre
A: 

Why not tie the functionality to zone security. That way, for trusted zones, developers of web apps can create robust functionality like what is being described here.

Tom
A: 

What about to use the specialized version of the "bind" command "one". Once the event handler executes the first time, it’s automatically removed as an event handler.

$(window).one("beforeunload", BeforeUnload);

Riga