Reputation: 3174
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.
Upvotes: 1
Views: 674
Reputation: 6674
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>
Upvotes: 1