1

I've seen a lot of question on this and the solution seems to be

window.onbeforeunload

But i've tried it, seems to work great to warn the user before the pages unload, but there is no way i've seems to be able to set the message in the message box.

i've tried this :

window.onbeforeunload = function (){ return "test";}

But I've got the default message of the browser.

I'm using the french version of firefox 8.0

Thanks all

GregM
  • 2,634
  • 3
  • 22
  • 37
  • possible duplicate of [How to display onbeforeunload dialog when appropriate?](http://stackoverflow.com/questions/2663728/how-to-display-onbeforeunload-dialog-when-appropriate) – Jakub Jan 11 '12 at 16:48
  • No i've look at it and tried it it's not working – GregM Jan 11 '12 at 16:49
  • You should include the relevant source code. How are you displaying the message? Is it a standard alert? Is it a modal alert? Some code would help. – Alex Morales Jan 11 '12 at 16:49
  • 1
    You will never able to achieve this on Opera Browser. – Shankar Narayana Damodaran Jan 11 '12 at 16:49
  • @Alex Morales, the onbeforeunload generate the alert – GregM Jan 11 '12 at 16:53
  • 1
    Yes I just saw that. Apparently there's no longer any way to use this event to add a custom message to the browser dialog in FireFox. They removed it because they deemed it a security issue. Here's the link: https://bugzilla.mozilla.org/show_bug.cgi?id=588292 – Alex Morales Jan 11 '12 at 17:00
  • @Alex - thanks for finding that bug. I read through the comments and it's a shame Mozilla decided to due away with the custom text. I feel their reasons are pretty paranoid. Oh well. – Matt Greer Jan 11 '12 at 17:08

1 Answers1

2

Firefox does not allow you to change the message, but webkit (Chrome/Safari) does. In webkit, if you return a string, in your onbeforeunload handler, it will interpret that as "pop a warning dialog" with your string as the message.

Sadly onbeforeunload is quite raw and not that great. Hopefully browsers figure out a better way to do this in the near future.

Matt Greer
  • 60,826
  • 17
  • 123
  • 123
  • thanks, so there is no way to prevent a user (with my own message) of using the back button in a browser – GregM Jan 11 '12 at 16:54
  • Not with your own message, no. But the default message is better than nothing. Most users now aday seem to be aware of this workflow and will know they are leaving unsaved changes behind. – Matt Greer Jan 11 '12 at 16:55
  • Well +1 on this, hope somebody will show me a way to accomplish this :S – GregM Jan 11 '12 at 16:57
  • Hopefully there is a trick out there. But I'm doubtful. You are not allowed to pop an alert inside `onbeforeunload`, and if you were to update an area on the page itself, the user probably won't see it. – Matt Greer Jan 11 '12 at 16:59