window.open的子项不在父窗口中调用函数

时间:2011-04-14 19:16:02

标签: javascript google-chrome

parent.html

<html>
    <head>
        <script type="text/javascript">
            function openWin()
            {
                myWindow=window.open('child.html','','width=200,height=100');
            }
            function callback(){
                alert("test");
            }
        </script>
    </head>
    <body>
        <input type="button" value="Open 'myWindow'" onclick="openWin()" />
    </body>
</html>

child.html

<html>
    <head>
       <script type="text/javascript">
          window.opener.callback();
       </script>
    </head>
    <body>
    </body>
</html>

问题是子页面在FF,IE中调用了父页面的回调函数,但在Chrome中却没有。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

由于Chrome安全性错误而导致问题发生。域,协议和端口必须匹配。但是当从本地文件系统打开页面时也会发生这种情况。

从服务器打开你的页面,应该没有任何问题。

答案 1 :(得分:0)

问题可能是chrome运行javascript的方式。 Chrome有时会如此快速地运行js,甚至DOM还没有准备好进行操作。在您的child.html中试试这个

<script type="text/javascript">
setTimeout(function(){window.opener.callback();}, 100);
</script>

我不确定这是否是您的确切问题,我在chrome上遇到了jQuery.ready()的问题。

相关问题