confirm alert coming twice called when onbeforeload event
I have a confirm box on beforeload event - the code is as follows:
window.onbeforeunload = function(){
//alert('leaving ... ' + is_edited );
if (is_edited == true) {
return confirm('Are you sure you want to navigate a开发者_运维问答way from this page? \n\n Press OK to continue, or Cancel to stay on the current page.');
}
};
The problem is that - the confirm alert is showing twice. I have searched on stackoverflow and got many questions about this issue. But mine is somewhat different. It is not the same confirm alert showing twice. For FF the message is same but for Crome in second confirm box the message is false
.
Thanks in advance.
The "onbeforeunload" handler is supposed to return a string. Whether the string will be shown to the user or not is up to the browser.
Your function returns the result of the "confirm()" call, a boolean. Firefox 4 does not show the returned string from the "beforeunload" handler, while other browsers still do. Thus your function puts up a "Confirm" box, gets the result, and returns it. The browser then puts up the "beforeunload" confirmation, which in the case of some browsers will include the stringified value returned from your handler (either "true" or "false").
Here is the Mozilla documentation for the event and its handler.
It's better for you to use jQuery here for cross browser functionality.
jQuery UI Dialog OnBeforeUnload
精彩评论