Call container page function from child iframe

The simplest way i got to call a function that exists in the iframe container page is using:

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>title>
head>
<body>
    <form id="form1" runat="server">
    <div>
        <script language="javascript" type="text/javascript">
            function AlertFromParent() {
                alert('I am parent');
            }
        script>
        <iframe src="IFramePage.aspx" id="myIframe">
        iframe>

    div>
    form>
body>
html>

 

 

Notes:

– This function is working just if the 2 pages ‘the parent and the child’ are in the same domain name, and if they are in 2 different domains you will get permission denied error.

– If you will run the javascript code inside the iframe on load ‘as in the previous example’, make sure that the parent script function exists before the iframe tag, because the iframe will break loading the page till it finish loading and everything after it will wait till it completely loads.

This entry was posted in Technical. Bookmark the permalink.