1

I have a site that loads an iframe. Code in the parent window registers an onbeforeunload listener.

Currently the iframe contains a button with a click handler that fires off:

window.open(/#/frag,'_parent')

In all browsers I have tested (besides IE8/IE9) this has the desired behavior of loading the parent fragment without triggering the onbeforeunload event. however, if I change the iframe's click hander to fire off:

parent.location.href='/#/frag'

it works as desired in IE 8/9 as well.

Can anyone explain this behavior?

I included a simple example below:

index.html

<!DOCTYPE html>
<html>
    <head>
        <script>
            function registerUnloadListener(){
                window.onbeforeunload = function (e) {
                    return "onbeforeunload";
                };
            }
        </script>
    </head>

    <body onload="registerUnloadListener()">
        <iframe width="100%" src="iframe.html"></iframe><br/>
    </body>
</html>

iframe.html

<!DOCTYPE html>
<html>
    <head>
        <script>
            function setParentLocation(frag){
                parent.location.href = frag;
            }
            function openWindowInParent(frag){
                window.open(frag,'_parent');
            }
        </script>
    </head>
    <body>
        onbeforeunload triggered (as expected) -->
        <a href="javascript:void(0);" onclick="setParentLocation('/')">
            parent.location.href = '/'
        </a><br/>

        onbeforeunload NOT triggered (as expected) -->
        <a href="javascript:void(0);" onclick="setParentLocation('/#/frag')">
            parent.location.href = '/#/frag'
        </a><br/>

        onbeforeunload triggered for IE8/IE9 only (unexpected) -->
        <a href="javascript:void(0);" onclick="openWindowInParent('/#/frag')">
            window.open("/#/frag",'_parent')
        </a>
    </body>
</html>

Thanks in advance for any feedback.

mjj1409
  • 3,075
  • 6
  • 28
  • 28
  • Are you asking a form of the question "why does Internet Explorer do something wrong or something dumb?" Internet Explorer is a force of nature and explanations don't help much. – Pointy Jul 26 '14 at 15:41
  • @Pointy I guess I'm looking to confirm that IE is doing it wrong and the other browsers are doing it right. – mjj1409 Jul 26 '14 at 17:18

1 Answers1

1

Try adding return false; to your anchors' onclick event. It is a known issue that anchors with href="javascript:void(0); trigger onbeforeunload in IE, but adding return false; to the click handler fixes that.

<a href="javascript:void(0);" onclick="openWindowInParent('/#/frag'); return false;">
    window.open("/#/frag",'_parent')
</a>
p e p
  • 6,593
  • 2
  • 23
  • 32
  • @p e p Thanks, that is helpful information to have, but it isn't preventing the **onbeforeunload** in my case. – mjj1409 Jul 28 '14 at 17:22
  • I wonder if the difference is due to having the link within a frame. Good luck, I'd like to hear if you are able to resolve this. – p e p Jul 28 '14 at 17:50