简体   繁体   English

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

[英]Child of window.open doesn't call function in parent window

parent.html 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 child.html

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

And the problem is that the child page calls parent's page callback function in FF, IE but not in Chrome. 问题是子页面在FF,IE中而不是在Chrome中调用父页面的回调函数。

Any idea ? 任何想法 ?

Problem happens because of Chrome security error. 发生问题是由于Chrome安全性错误。 Domains, protocols and ports must match. 域,协议和端口必须匹配。 But it also happens when page is open from local files system. 但是从本地文件系统打开页面时也会发生。

Open your page from server, it should be without any problems. 从服务器打开页面,应该没有任何问题。

The problem might be the way how chrome run javascript. 问题可能是chrome如何运行javascript的方式。 Chrome sometimes run the js so fast and early and even the DOM is not ready for manipulation. Chrome浏览器有时会如此快且早地运行js,甚至DOM还没有准备好进行操作。 Try this in your child.html 在您的child.html中尝试

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

I am not sure if this is the exact problem for you, I encountered this problem with jQuery.ready() on chrome. 我不确定这是否对您确切,我在chrome上使用jQuery.ready()遇到了此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM