JS in the IFRAME Frame child page calls the parent window function

Source: Internet
Author: User

In two different situations:

The first type: Invoke the parent page JS function in the IFRAME child page:

The child page calls the parent page function just write window.praent on it. For example, to call a () function, you can write:

The code is as follows Copy Code

WINDOW.PRAENT.A (); A () as a method;

The second type: in the IFRAME Child parent page Call the child page JS function:

This is a little more complicated, we can use:

The code is as follows Copy Code

document.getElementById (' Ifrtest '). contentwindow.b ();

Note: Ifrtest is the id,b () of the IFRAME frame as the JS function of the sub page. Contentwindow is the specified frame or the Window object where the IFRAME, ie can be omitted

Example

Parent page: index.html (using IFRAME to include subpages child.html)

The code is as follows Copy Code

<script type= "Text/javascript" >
<!--
function Toshow (msg) {
Alert ("Here is the parent page:" +msg);
}
-->
</script>
<body>
<iframe background= "Blue" src= "child.html" mce_src= "child.html" ></iframe>
</body>

Sub-page child.html (invoke parent page Toshow method)

The code is as follows Copy Code

<script type= "Text/javascript" >
<!--
function Doparent () {
Window.parent.toshow (' Ccccchild ');
}
-->
</script>
<body>
This are child frame!!
<input type= "button" value= "click" onclick= ' doparent () ' ></input>
</body>

Add: Get elements from the IFRAME child page in the parent page:

var child = document.getelementbyidx_x ("MainFrame"). Contentwindow;//mainframe this ID is the ID of the parent page iframe
child.document;//gets the document object in the child page;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.