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

Example of calling js function from iframe sub-parent page

高洛峰
Release: 2016-12-24 16:57:34
Original
1408 people have browsed it

1. If 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, when calling the a() function, it is written as:

window.parent.a();
Copy after login

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

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

jQuery方法为: 

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

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

2. The iframe parent page calls the subpage js function

这个就稍微复杂一些,下面的方法支持ie和firefox浏览器: 

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

子页面取父页面中的标签中的值,比如该标签的id为“test”,则: 

document.getElementById("test").value;
Copy after login

Note: ifrtest is the id of the iframe frame, 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.


For more related articles on examples of js function calls for iframe child and parent pages, please pay attention to the PHP Chinese website!


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