Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript window.onbeforeunload,但对于帧?_Javascript_Frame_Frameset_Onbeforeunload - Fatal编程技术网

Javascript window.onbeforeunload,但对于帧?

Javascript window.onbeforeunload,但对于帧?,javascript,frame,frameset,onbeforeunload,Javascript,Frame,Frameset,Onbeforeunload,我正试图发布一条关于离开网页的警告消息,但它嵌套在一个框架集中 显然,如果我在浏览器中将网页作为根页面加载,window.onbeforeunload会触发,但在框架集中加载时不会触发 编辑:它在Chrome 7或Safari 5中不起作用,但在Internet Explorer 8和FireFox 3.6中起作用。是否有一个修复/解决方案,使我可以让它在Chrome 7和/或Safari 5中工作 我做错了什么 这就是我所尝试的: function closePageWarning() {

我正试图发布一条关于离开网页的警告消息,但它嵌套在一个框架集中

显然,如果我在浏览器中将网页作为根页面加载,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脚本的页面)

函数closePageWarning()
{
return“您确定要离开此页吗?”;
}
window.onbeforeunload=关闭页面警告;
test2.html(框架集)

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


如何测试:

  • 在浏览器中加载test1.html,然后单击链接,注意您得到了问题
  • 在浏览器中加载test2.html,单击任意一个链接,注意您没有问题
测试地点:

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

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

奇怪,从没听说过。您能确认准确的代码在框架集之外工作吗?您在哪些浏览器中进行了测试?@Pekka:onbeforeunload是Microsoft的一个事实标准,在所有主要浏览器中都实现了:。@Eric否,我的意思是它在iframe中不起作用。不过,如果他在子文档中声明
onbeforeunload
,它应该可以工作,不是吗?好像WebKit是罪魁祸首。注意我并没有卸下整个窗口,只是卸下一个框架。@Lasse:对不起,我误解了。我认为你是对的。看起来这个功能有一些WebKit错误。好吧,我想有支持总比没有支持好。我将尝试为其他浏览器找到一种不同的方式。
<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>
<html>
    <frameset cols="20%, 80%">
        <frame name="menu" src="test3.html" />
        <frame name="content" src="test1.html" />
    </frameset>
</html>
<html>
    <body>
        <a target="content" href="http://www.bitbucket.org">bitbucket</a>
    </body>
</html>