Internet Explorer calling window.onbeforeunload on window.open and AJAX calls Internet Explorer calling window.onbeforeunload on window.open and AJAX calls ajax ajax

Internet Explorer calling window.onbeforeunload on window.open and AJAX calls


Another and probably simpler option is to return false when you open the popup:

<a onclick="window.open(...); return false;" href="javascript:;" >my link</a>

This seems to stop IE from thinking you're leaving the page and triggering the event. All the other options weren't particularly viable for me.


OK, I have been having this issue. I have a (rather messy) work around for it.

In my case, I want to block navigation away sometimes, and not others.

So, I am setting a flag on the window to tell me if I want it blocked. So where you are doing your window.open, just before that, do 'window.allowExit=true' then in the onbeforeunload, check for window.allowExit = true.

I have the java script (SHowHelp) being kicked off from a link:

<a href="javascript:ShowHelp('argument')" >HERE</a>

onbeforeunload is called BEFORE the ShowHelp, so i used the onclick to set the flag

<a onclick="window.allowExit = true;" href="javascript:ShowHelp('argument')" >HERE</a>

Ugly as sin, but it seems to work!


This is not a solution, but an explanation for anybody who is interested. I just ran a quick test in IE 7, and it fires the onebeforeunload event any time a link is clicked unless the HREF goes somewhere on the same page: i.e. unless it contains a #. So my guess is that the IE engineers were thinking that when somebody clicks a link that is not to somewhere else on the page, then they must be leaving the page, in which case the page is about to unload. Needless to say, there are obvious problems with this thinking.