如何从子窗口jquery调用父窗口函数?

时间:2014-08-02 19:23:08

标签: javascript jquery html5

我只需要在用户关注子窗口时调用父窗口中的函数。 我在父窗口中有这段代码,

<html> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script language="Javascript" type="text/javascript">
        function CallParent()
        {
            alert(" Parent window Alert");
        }
    </script>
    <body> 
        <a href="javascript:void(0);" NAME="My Window Name" title=" My title here " onClick=window.open("child.html","Ratting","width=550,height=170,0,status=0,");>Click here to open the child window</a>
    </body> 
</html>

和下面的代码在我的子窗口中,

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script language="Javascript" type="text/javascript">
            jQuery(document).ready(function(){  
                window.opener.CallParent();
            });
        </script>
    </head>
    <body> 
        <h2>This is Child window</h2> 
    </body> 
</html>

所以..在这种情况下,我认为{Child}窗口打开后会激活CallParent()。但似乎没有用。 任何人都可以给我任何提示,使这个脚本工作,或任何更好的方法来做到这一点。

3 个答案:

答案 0 :(得分:14)

使用此

window.parent.CallParent();

而不是

window.opener.CallParent();

window.parent包含对当前窗口或子帧的父级的引用。

如果窗口没有父窗口,则其parent属性是对自身的引用。

当在<iframe><object><frame>中加载窗口时,其父窗口是嵌入窗口的元素的窗口。

参考:https://developer.mozilla.org/en-US/docs/Web/API/Window/parent

答案 1 :(得分:11)

尝试以下内容:

<强> parent.html

<html> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script language="Javascript" type="text/javascript">
        window.CallParent = function() {
            alert(" Parent window Alert");
        }
    </script>
    <body> 
        <a href="javascript:void(0);" NAME="My Window Name" title=" My title here " onClick=window.open("child.html","Ratting","width=550,height=170,0,status=0,");>Click here to open the child window</a>
    </body> 
</html>

<强> child.html

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script language="Javascript" type="text/javascript">
            jQuery(document).ready(function(){  
                opener.CallParent();
            });
        </script>
    </head>
    <body> 
        <h2>This is Child window</h2> 
    </body> 
</html>

您不应该在父级上执行此操作,否则opener.CallParent()将无效,执行window.CallParent使CallParent可用作窗口范围:

function CallParent() {
  alert(" Parent window Alert");
}

然后,您只需致电opener.CallParent();而不是window.opener.CallParent();

答案 2 :(得分:0)

我使用了以下代码 parent.html

<html> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script language="Javascript" type="text/javascript">
        function CallParent()
        {
            alert(" Parent window Alert");
        }
    </script>
    <body> 
        <a href="javascript:void(0);" NAME="My Window Name" title=" My title here " onClick=window.open("child.html","Ratting","width=550,height=170,0,status=0,");>Click here to open the child window</a>
    </body> 
</html>

child.html

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script language="Javascript" type="text/javascript">
           window.opener.CallParent();
        </script>
    </head>
    <body> 
        <h2>This is Child window</h2> 
    </body> 
</html>