Hashchange event not working as expected in IE iframe

1.1k Views Asked by At

The hashchange event in IE doesn't seem to fire when triggered from within an iframe from an anchor targeted to its same-domain parent window. Consider the following super basic HTML pages (intentionally not using libraries like jQuery for simplicity).

parent.htm

<!DOCTYPE html>
<html>
<head>
    <title>Parent</title>
    <script type="text/javascript">
        window.onhashchange = function () {
            alert(window.location.hash);
        };
    </script>
</head>
<body>
    <p><a href="#A">A</a></p>
    <iframe src="child.htm"></iframe>
    <script type="text/javascript">
        document.write('<p>Loaded at ' + new Date() + '</p>');
    </script>
</body>
</html>

child.htm

<!DOCTYPE html>
<html>
<head>
    <title>Child</title>
</head>
<body>
    <p><a href="parent.htm#B" target="_top">B</a></p>
    <p><a href="parent.htm#C" target="_parent">C</a></p>
    <p><a href="#" onclick="top.location='parent.htm#D';">D</a></p>
</body>
</html>

In Chrome and Firefox, all four links (A,B,C,D) will result in an alert. In IE, links A and D will trigger the alert, but B and C will cause a full page reload with no alert. I couldn't find any documentation that explains this as expected behavior.

I can hack in a jQuery shim that fixes IE child links by attaching event handlers, but is there any way I can get IE to recognize the hashchange?

0

There are 0 best solutions below