我有一个加载 iframe 的网站。父窗口中的代码注册了一个onbeforeunload监听器。
目前,iframe 包含一个带有触发点击处理程序的按钮:
window.open(/#/frag,'_parent')
在我测试过的所有浏览器中(除了IE8/IE9),这具有加载父片段而不触发onbeforeunload事件的所需行为。但是,如果我将 iframe 的点击处理程序更改为触发:
parent.location.href='/#/frag'
它在 IE 8/9 中也可以正常工作。
谁能解释这种行为?
我在下面包含了一个简单的示例:
索引.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>
提前感谢您的任何反馈。