'Javascript: onBeforeUnload() behaviour in IE and Chrome
I have this piece of code:
window.onbeforeunload = function(){
doSomething();
return "Are you sure you want to leave";
}
In Firefox when user leaves the page, doSomething() gets executed first and then the alert pops up.
where as in IE and Chrome the alert pops up first and then if the user decides to stay doSomething()
gets executed.
Is there a way to make IE and Chrome behave like Firefox. I want to execute doSomething() before showing the pop up.
Solution 1:[1]
What version of Firefox are you using? As far as I know none of the browsers are supposed to let you do anything on onBeforeUnload other than pass text to be included in the confirm dialog that pops up - so you can't prevent the user from leaving your site.
You shouldn't rely on this event to ensure anything happens when the user leaves your page. If it's that important assign a click event handler to everything that can navigate away from your page (you'll still lose out on the browser navigate buttons and address bar changes)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tomfumb |
