Consider the following three pages.
Foo.htmlis opened locally in the browser, and so has a URL with prefixfile:///.Bar.htmlis in the same directory hasFoo.html.Bar2.htmllives in/var/wwwand I am runningApacheonlocalhost.
Foo.html
<html>
<head>
<script>
foo = function() {
alert("frame changed");
};
</script>
</head>
<body>
<iframe width="200" height="300" src="Bar.html" id="my-iframe" onLoad="foo" />
</body>
</html>
Bar.html
<html>
<body>
<iframe width="200" height="300" src="http://localhost/Bar2.html" id="my-iframe" />
</body>
</html>
Bar2.html
<html>
<head>
<script>
if (top.location != self.location){
parent.location = self.location;
}
</script>
</head>
<body>
<button type="button" onclick="document.location.href='http://bing.com'">Hello World</button>
</body>
</html>
When Foo.html is loaded in Firefox, by running firefox /path/to/Foo.html on the command line, the frame busting code in Bar2.html breaks out of Bar.html. At this point, the user gets an alert frame changed.
When I click the button, the iframe changes (the button vanishes), but I do not get an alert.
Why is the onLoad not firing the second time when the page changes?
Your script
will run on Bar2.htm and change the location of its parent (previously Bar.htm). Then the alert comes. Then, the script will run in the parent (previously Bar.htm) but top.location is still not equal to self.location . (top.location is now equal to Foo.htm and self.location is equal to Bar.htm ) so when you click the button, the parent page has been redirected to Bar2.htm . Meaning the alert code and the iframe is gone.