Call a parent window function from an iframe

Did you ever want to call a code/website that belongs to the parent window, and that too from the child window? Well, some good news for new. You will be able to do your desired job. The window will get opened by using the iframe, and the communication could be done with the parent window from the child window.

To call a parent window function, use the following syntax and also refer to the code given below.
Syntax:

<a onclick="parent.abc();" href="#" > Call the parent window here </a>

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
   
<html>
   
<head>
    <title>Calling a parent window function from an iframe</title>
   
    <script type="text/javascript"
    </script>
    <script type="text/javascript">
        $(function() {
            $('[id*=iframe1]').load(function() {
   
                //call here your javascript function
                myfunction();
            });
        });
   
        function myfunction() {
            alert('Function called after the IFrame load')
        }
    </script>
   
</head>
   
<body>
   
    <center>
        <h2 style="color:green">GeeksforGeeks</h2>
        <p>Calling a parent window function from an iframe</p>
        <form id="form1" runat="server">
            <div>
                <!-- Calling a parent window function from an iframe -->
                <iframe id="iframe1" src="https://ide.geeksforgeeks.org/"
                        width="600" height="450" frameborder="0"
                              style="border:0" scrolling="no"></iframe>
            </div>
        </form>
    </center>
</body>
   
</html>

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.