The iframe child parent page calls the js function example

  • 2020-03-26 23:50:15
  • OfStack

1. The iframe child page calls the js function of the parent page

The child page calls the parent page function by simply saying window.praent. For example, if I call the function a(), I will write:
 
window.parent.a(); 

The child page takes the value of the tag in the parent page, for example, the tag id is "test", then:
 
window.parent.document.getElementById("test").value; 

jQuery The method is:  

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

However, I found that this method did not work in chrome! It took me a while to figure out that in chrome 5+, window.parent doesn't work with the file:// protocol, but it does work with the http:// protocol once it's released. This method supports Internet explorer and firefox browsers.

2. The iframe parent page calls the child page js function
 
 This is a little more complicated, and the following method supports it ie and firefox Browser:  

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

 The child page takes the value of the tag in the parent page, such as the tag's id As a" test " , Is:  

document.getElementById("test").value; 

Note: ifrtest is the id of the iframe frame, and b() is the js function of the child page. The contentWindow property is the window object where the specified frame or iframe resides, which can be omitted from IE.

Related articles: