views:

633

answers:

4

Ok, I have spent a while on this problem and this is what I have gathered:

  1. If you make an AJAX call in IE7 and you have a window.onbeforeunload function specified, it calls the onbeforeunload function.

  2. If you try to open a new window with window.open WITHOUT disturbing the current window, the onbeforeunload gets called.

Does anyone know how to stop this? I even tried setting a variable to TRUE and check that variable in my onbeforeunload function and it still dosent work! I just need to be able to stop the execution of that method for AJAX calls and new window calls.

Thanks!

A: 

did you try removing handler from "onbeforeunload" event before calling to window.open? This can help but I never tested it.

Umair Ashraf
A: 

I figured that you just need to unset the window.onbeforeunload function before you do anything and then put it back when you're done.

I just ended up disabling the feature in IE.

agentfll
+1  A: 

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!

Jon
A: 

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.

Sid_M