window.onbeforeunload,但对于一个框架?

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)

测试文件:

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>
Run Code Online (Sandbox Code Playgroud)

test2.html(frameset)

<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)

test3.html(菜单,用脚本触发框架更改)

<html>
    <body>
        <a target="content" href="http://www.bitbucket.org">bitbucket</a>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

如何测试:

  • 在浏览器中加载test1.html,然后单击链接,注意您收到了问题
  • 在浏览器中加载test2.html,点击任一链接,注意你没问题

测试中:

  • 谷歌Chrome 7.0.517.44 - 不起作用
  • Internet Explorer 8.0.7600.16385 - 有效
  • Safari 5.0.2 - 不起作用
  • FireFox 3.6.12 - 有效

Eri*_*sen 1

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