Js accesses functions and elements in iframe

Source: Internet
Author: User

The iframe framework is a feature that we often use, such as using an ifame file to upload or using iframe to call pages. The following describes how to use js to access functions and elements in iframe, I hope this tutorial will be helpful to you.

Access functions in iframe

The Code is as follows: Copy code

Document. getElementById ('commentiframework'). contentWindow. hasLogined ();

CommentIframe is the id of iframe.

To be executed in window. onload, I don't know if you understand it.

Example 1. Access js functions in iframe

Code

The Code is as follows: Copy code


Http://www.bKjia. c0m/--> <a href = "#" onclick = "window. frames ['frame1']. MyNext ()"> aa </a>
<Iframe id = "frame1" src = "print.html"> </iframe>
Print.html page
<Script language = "javascript" type = "text/javascript">
Function MyNext ()
{
Alert (1 );
}
</Script>

So how do we get the elements or IDs in the page?

Example 3. Access elements in iframe using JS

1. First, how do I access elements in iframe? See the following code:
(1100001.htm source code:

The Code is as follows: Copy code
<Input type = button value = "test" onclick = "self.frames{'a'}.doc ument. all. mybutton. disabled = true">
<Iframe name = 'A' src = "2.htm" id =" iframe2 "> </iframe>
(22.162.htm source code
<Input type = button value = "mybutton" id = "mybutton">
<Script language = javascript>
Function showInfo () {alert ();}
</Script>

------------------------------------------------
The JS functions in callback are written as follows:
Self. frames ['a']. funtionname (param)

Call the JS function in 2.htm in 1.htm: iframe2.showInfo ();

2. problem I encountered: the elements in the iframe cannot be accessed according to the above method when creating a project !! The code is copied to change the volume name. Why not?
Later, I found a difference in nature: My iframe page is not a site page, it is directed to the url in the site!
After saving the website page to this site, you can, huh, huh!
That is to say, for an iframe page, if it is an out-of-Site Page, this site has no control over it, and even the page elements cannot be accessed.

Dynamically change the iframe height

The Code is as follows: Copy code

Function changeParent (){
If (frameid1! = "" & Parent! = Null)
Parent.doc ument. getElementById ("frameid1"). style. height = document. body. scrollHeight;
}

Related Article

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.