Busted Mug

A blog that documents solutions to the most frustrating problems that occur during development in technologies such as Java, XML, AJAX, SQL, CSS and others that make me want to throw my coffee mug against the cube wall.

Thursday, January 25, 2007

OnBeforeUnload, IE7, Assigning event handlers to null and the problems that arise

This IE7 issue almost totally screwed me at work. Image if a JS designed to keep users from leaving a page before placing their order suddenly started telling all users that they were leaving and their orders weren't being placed, even though they were. Ouch.. that'd probably have someone gunning for my job. But that almost happened. Here's how MSoft and sucky IE7 almost did it to me: I had a script like this.
    <script>
      
window.onbeforeunload confirmExit;
      function 
confirmExit()
      {
           
return "Leaving this page will cause all unplaced orders to be discarded.  Are you sure you want to leave the page?";
      
}
    
</script>
    
    . . . . .

    
<input type="image" src="/lgrequest.gif" onclick="javascript: window.onbeforeunload = null;     document.forms[0].submit();" />
Apparently IE7 doesn't like the part where I set onBeforeUnload = null; SO it just ignored that. The result would have been disaster but for a stroke to testing luck. Here's the simple yet crucial change that I made:
    <script>
    
var goodExit = false;
      window
.onbeforeunload confirmExit;
      function 
confirmExit()
      {
          
if (!goodExit)
               
return "Leaving this page will cause all unplaced orders to be discarded.  Are you sure you want to leave the page?";
      
}
    
</script>

    . . . . . 

    
<input type="image" src="<%=AppService.BASEDIR%>/images/lgrequest.gif" onclick="javascript: goodExit = true; window.onbeforeunload = null; document.forms[0].submit();" />
I know I have mentioned it before but IE7 sucks! I use it at work and I must admit the new tab interface works nicely, but all the crap they changed makes me say "IE7 SUCKS"

4 Comments:

At 2:37 PM, Anonymous ryan u. said...

I've actually found a way to detach the onbeforeunload event handler without having to use a global flag and all that. However, it's only compatible for Firefox (2?) and IE7. The product I'm working on is only supported for these two browsers, but others might need to support other, older variants. Nevertheless:

Firefox 2:
window.onbeforeunload = null;

IE 7:
window.document.body.onbeforeunload = ";";

Hope this helps someone out there, as it did for me.

 
At 7:18 AM, Anonymous Mathieu Boillat said...

For IE, you can also use :
window.document.body.onbeforeunload = null;

Same thing for onunload event :
window.document.body.onunload = null;

But IE still sucks !

 
At 6:08 PM, Anonymous Anonymous said...

[url=http://www.kfarbair.com][img]http://www.kfarbair.com/_images/_photos/photo_big8.jpg[/img][/url]

בית מלון [url=http://www.kfarbair.com]כפר בעיר[/url] - אינטימיות, [url=http://kfarbair.com/services.html]שקט[/url] . אנחנו מציעים שירותי אירוח מיוחדים גם ישנו במקום שירות חדרים המכיל [url=http://www.kfarbair.com/eng/index.html]ארוחות רומנטיות[/url] במחירים מיוחדים אשר מוגשות ישירות לחדרכם...

לפרטים אנא לפנות לעמוד המלון - [url=http://kfarbair.com]כפר בעיר[/url] [url=http://www.kfarbair.com/contact.html][img]http://www.kfarbair.com/_images/apixel.gif[/img][/url]

 
At 7:49 PM, Anonymous Anonymous said...

is rollo weeks dating a girl named jesse [url=http://loveepicentre.com/]singles all together minnesota[/url] ancientbrit personals http://loveepicentre.com/ cross road dating

 

Post a Comment

<< Home