Category ArchiveJavascript
Javascript & Programming & Web 12 Oct 2007 12:40 pm
onbeforeunload IE7 Woes!
Was just having a problem getting the IE7 onbeforeunload event to be canceled. Found this blog about it: OnBeforeUnload, IE7, Assigning event handlers to null and the problems that arise.
Pretty simple, just wrap the return with a boolean:
JAVASCRIPT:
-
var goodExit = false;
-
window.onbeforeunload = confirmExit;
-
function confirmExit()
-
{
-
// normally this if-check wouldn't be here,
-
// cause the event handler was nulled elsewhere.
-
// But IE7 doesn't respect the null, and
-
// wants the handler to not return anything
-
// (not even null).
-
if (!goodExit)
-
{
-
return "Leaving this page will cause all unplaced orders to be discarded."+
-
" Are you sure you want to leave the page?";
-
}
-
// notice no return statement here!
-
}
-
</script>
-
-
. . . . .
-
-
<input type="image" src="<%=AppService.BASEDIR%>/images/lgrequest.gif" onclick="javascript: goodExit = true; window.onbeforeunload = null; document.forms[0].submit();" />
This example was stolen from Brandon Himes and his blog, The Busted Mug. Thank you Brandon!