Home > Web Front-end > JS Tutorial > body text

Example of calling js function in iframe child and parent pages_javascript skills

WBOY
Release: 2016-05-16 17:16:55
Original
1044 people have browsed it
1. The iframe subpage calls the parent page js function

The subpage only needs to write window.praent to call the parent page function. For example, to call the a() function, it is written as:
Copy the code The code is as follows:

window. parent.a();

The child page takes the value in the tag in the parent page. For example, the id of the tag is "test", then:
Copy code The code is as follows:

window.parent.document.getElementById("test").value;

jQuery method For:

$(window.parent.document).contents().find("test").val();

But I found it under the chrome browser This method is invalid! After checking for a long time, I found out that in chrome 5, window.parent cannot run in the file:// protocol, but after it is released, it can run in the http:// protocol. This method supports IE and Firefox browsers.

2. The iframe parent page calls the child page js function
Copy the code The code is as follows :

This is a little more complicated. The following method supports IE and Firefox browsers:

document.getElementById('ifrtest').contentWindow.b();

The child page takes the value in the tag in the parent page. For example, the tag's id is "test", then:

document.getElementById("test").value;

Note: ifrtest is the id of the iframe, and b() is the subpage js function. The contentWindow attribute is the window object where the specified frame or iframe is located, and can be omitted under IE.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template