Search code examples
ajaxinternet-explorerwindow.openonbeforeunload

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


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.


Solution

  • 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.