Search code examples
javascripthtmlonbeforeunload

Best way to warn user that he is quitting the page


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


Solution

  • 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.