ang*_*son 5 javascript onbeforeunload frameset frame
我试图提出关于离开网页的警告信息,但它嵌套在框架集中.
如果我在浏览器中将网页加载为根页面,则显然会触发window.onbeforeunload触发器,但在框架集内加载时不会触发.
编辑:它在Chrome 7或Safari 5中不起作用,但它适用于Internet Explorer 8和FireFox 3.6.是否有修复/解决方法,以便我可以在Chrome 7和/或Safari 5中使用它?
我究竟做错了什么?
这是我试过的:
function closePageWarning()
{
return "Are you sure you want to leave this page?"
}
window.onbeforeunload = closePageWarning
Run Code Online (Sandbox Code Playgroud)
测试文件:
<html>
<body>
<script language="javascript">
function closePageWarning()
{
return "Are you sure you want to leave this page?";
}
window.onbeforeunload = closePageWarning;
</script>
<a href="http://www.bitbucket.org">bitbucket</a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
<html>
<frameset cols="20%, 80%">
<frame name="menu" src="test3.html" />
<frame name="content" src="test1.html" />
</frameset>
</html>
Run Code Online (Sandbox Code Playgroud)
<html>
<body>
<a target="content" href="http://www.bitbucket.org">bitbucket</a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
如何测试:
测试中:
在框架的上下文中,window指的是框架,它是一种窗口。sowindow.onbeforeunload指的是frame中窗口的一个事件,当根页面卸载时不会触发该事件。您可以通过 引用根窗口window.top,或者通过 引用框架的直接父窗口window.parent。然而,这些不是标准属性,因此使用它们需要您自担风险!