window.onbeforeunload,但是对于一个框架呢?
我试图提出有关离开网页的警告消息,但它嵌套在框架集中。
显然,如果我将网页加载为浏览器中的根页面,则 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
测试文档:
test1.html (带有 unbeforeunload 脚本的页面)
<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>
test2.html (框架集)
<html>
<frameset cols="20%, 80%">
<frame name="menu" src="test3.html" />
<frame name="content" src="test1.html" />
</frameset>
</html>
test3.html (菜单,使用脚本触发框架更改)
<html>
<body>
<a target="content" href="http://www.bitbucket.org">bitbucket</a>
</body>
</html>
如何测试:
- 在浏览器中加载 test1.html,并单击该链接,请注意您收到问题
- 在浏览器中加载 test2.html,单击任一链接,请注意您没有收到任何问题
测试环境:
- Google Chrome 7.0.517.44 - 不起作用
- Internet Explorer 8.0.7600.16385 - 有效
- Safari 5.0.2 - 无效
- FireFox 3.6.12 - 有效
I'm trying to put up a warning message about leaving a webpage, but it is nested inside a frameset.
Apparently window.onbeforeunload triggers if I load the webpage as the root page in the browser, but doesn't trigger when loaded inside the frameset.
Edit: It doesn't work in Chrome 7 or Safari 5, but it works in Internet Explorer 8 and FireFox 3.6. Is there a fix/workaround so that I can get it working in Chrome 7 and/or Safari 5 as well?
What am I doing wrong?
This is what I tried:
function closePageWarning()
{
return "Are you sure you want to leave this page?"
}
window.onbeforeunload = closePageWarning
Test documents:
test1.html (page with unbeforeunload script)
<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>
test2.html (frameset)
<html>
<frameset cols="20%, 80%">
<frame name="menu" src="test3.html" />
<frame name="content" src="test1.html" />
</frameset>
</html>
test3.html (menu, trigger change of frame with script)
<html>
<body>
<a target="content" href="http://www.bitbucket.org">bitbucket</a>
</body>
</html>
How to test:
- Load up test1.html in your browser, and click the link, notice you get the question
- Load up test2.html in your browser, click either link, notice you get no question
Tested in:
- Google Chrome 7.0.517.44 - doesn't work
- Internet Explorer 8.0.7600.16385 - works
- Safari 5.0.2 - doesn't work
- FireFox 3.6.12 - works
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在框架的上下文中,
window
指的是框架,它是一种窗口。所以window.onbeforeunload
指的是框架中窗口的一个事件,在卸载根页面时不会触发该事件。您可以通过window.top
引用根窗口,或者通过window.parent
引用框架的直接父窗口。然而,这些不是标准属性,因此使用它们需要您自担风险!In the context of a frame,
window
refers to the frame, which is a kind of window. Sowindow.onbeforeunload
refers to an event of the window in the frame, which is not fired when the root page is unloaded. You can refer to the root window bywindow.top
, or to the direct parent window of the frame bywindow.parent
. However, these are not standard properties, so use them at your own peril!