5

我正在尝试发布有关离开网页的警告消息,但它嵌套在框架集中。

显然,如果我将网页作为浏览器中的根页面加载,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,单击任一链接,注意您没有问题

测试:

  • 谷歌浏览器 7.0.517.44 - 不工作
  • Internet Explorer 8.0.7600.16385 -有效
  • Safari 5.0.2 - 不工作
  • FireFox 3.6.12 -工作
4

1 回答 1

1

在框架的上下文中,window是指框架,它是一种窗口。sowindow.onbeforeunload是指框架中窗口的一个事件,在卸载根页面时不会触发。您可以通过 引用根窗口window.top,也可以通过 引用框架的直接父窗口window.parent。但是,这些不是标准属性,因此使用它们需要您自担风险!

于 2010-11-08T15:29:24.297 回答