IE issue with canceling the onBeforeUnload function
-
I am having an issue with canceling an event I added to the window's onBeforeUnload event, when the browser is IE (7,8, &9). In FireFox my RegisterOnSubmitStatement is sufficient to set a flag to skip the warning, but in IE that code gets called after the onBeforeUnload. Here is the code in question:
ScriptManager.RegisterStartupScript(page, typeof(Functions), "NavigateAway", @"
window.SuppressExitWarning = false;window.onbeforeunload = confirmExit; function confirmExit(evt) { var message = '" + JsEscape(message) + @"'; if (window.SuppressExitWarning == true) return; if (typeof evt == 'undefined') evt = window.event; if (evt) evt.returnValue = message; return message; };", true); ScriptManager.RegisterOnSubmitStatement(page, typeof(Functions), "AllowNavigateAway", @" window.SuppressExitWarning = true; ");
If you add an alert to the "AllowNavigateAway" script, you can see the behaviour in IE -> The onBeforeUnload message pops up, then the alert. I have tried several ways to solve it, but they all have a flaw of some sort. A) Adding a setting of the global variable on the OnClientClick event to all possible form submitters will invariably miss something. That is to say, I do not have any idea of what controls will be on the page and even if I iterate through the control tree, I am likely to miss something that it not usually a control that submits but has it's AutoPostBack set to true. (e.g. DropDownList) B) Using jQuery to attach a method to the submit of the form is not an option because CKEditor has locked it up. C) Tracking the postback through a hidden input fails because it is not updated until after the onBeforeUnload. Any thoughts or ideas would be greatly appreciated.