How to call parent window function from child window jquery?

前端 未结 3 1068
青春惊慌失措
青春惊慌失措 2020-12-03 16:54

i just need to call function in parent window while user is focusing on child window. i have this code in my parent window,

 
    

        
相关标签:
3条回答
  • 2020-12-03 17:31

    Use this

    window.parent.CallParent();
    

    instead of

    window.opener.CallParent();
    

    window.parent holds a reference to the parent of the current window or subframe.

    If a window does not have a parent, its parent property is a reference to itself.

    When a window is loaded in an <iframe>, <object>, or <frame>, its parent is the window with the element embedding the window.

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

    0 讨论(0)
  • 2020-12-03 17:52

    Try something like below:

    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>
    

    You should not do this on the parent, otherwise opener.CallParent() will not work, doing window.CallParent makes CallParent available as window scope:

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

    And then you can simply call opener.CallParent(); not window.opener.CallParent();.

    0 讨论(0)
  • 2020-12-03 17:53

    I used the following code 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>
    
    0 讨论(0)
提交回复
热议问题