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; } |