Javscript method of calling functions in an IFRAME frame page _javascript Tips

Source: Internet
Author: User

This article illustrates the method of calling the function in the IFRAME frames page in Javscript, this method is very simple, with this method we can achieve the value or modify the value of the IFRAME, the operation is very simple. Share to everyone for your reference. The implementation methods are as follows:

To access the functions within the IFRAME:

Copy Code code as follows:
document.getElementById (' Commentiframe '). contentwindow.haslogined ();

Commentiframe is the ID of the IFRAME.
To be executed inside the window.onload.

Examples are as follows:

1.html

Copy Code code as follows:
<a href= "#" onclick= "window.frames[' frame1 '". Mynext () ">aa</a>
<iframe id= "frame1" src= "2.html" ></iframe>

2.html page

Copy Code code as follows:
<script language= "javascript" type= "Text/javascript" >
function Mynext ()
{
Alert (1);
}
</script>

Click the Test button in 1.htm to invalidate the MyButton button in the 2.htm (IFrame page). It's so simple, hehe. If you want to call the JS function in 2.htm to write this:

Copy Code code as follows:
self.frames[' a '].funtionname (param)

In 1.htm, call the JS function in 2.htm: Iframe2.showinfo ();

Example Description:

Suppose there are 2 pages, index.html and inner.html. One of the index.html has an IFRAME, which is the src of the iframe pointing to inner.html.

All we have to do now is:

1. Call a JS method on the inner.html in index.html
2. Call a JS method on the index.html in inner.html

The implementation code is as follows:

Index.html:

Copy Code code as follows:
<script type= "Text/javascript" >
function ff () {
Alert (">>this is index ' s JS function index.html");
}
</script>
<body>
<div style= "Background:lightblue;" >
This is index page.
<input type= "button" value= "Run index ' s function" onclick= "FF ();"/>
<input type= "button" value= "Run inner page ' s function ' onclick= ' window.frames[' Childpage '].sonff (); '/>
</div>
<iframe id= "Childpage" name= "Childpage" src= "inner.html" width= "100%" frameborder= "0" ></iframe>
</body>

Inner.html:

Copy Code code as follows:
<script type= "Text/javascript" >
function Sonff () {
Alert (">>this is inner page ' s JS function");
}
</script>
<body>
<div style= "Background:lightgreen;" >
This is inner page.
<input type= "button" value= "Run index ' s function" onclick= ' parent.window.ff (); '/>
<input type= "button" value= "Run inner page ' s function" onclick= "SONFF ();"/>
</div>
</body>

I hope this article will help you with your JavaScript based Web programming.

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.