IE7 cancel onbeforeunload

I was using the onbeforeunload event to confirm the user wants to leave the page with changes, something similar to when you create a new mail in Gmail. Supposedly, setting the event.returnValue to a String will activate a browser prompt when you leave the page. Setting it null makes it not prompt, after I decided that there were no changes made.

But that only worked in FF2. IE showed a “null” as the message. No matter what I set for the event.returnValue in IE, it prompted. I even tried setting the returnValue to false, and event.cancelBubble = true, but nothing worked.

Finally by trial and error, the only way to make IE not prompt is to NOT set it at all. Don’t call event.returnValue = something. This works for FF2 too, so I’m done.

Leave a Reply