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:

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

jQuery The method is:  


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:  


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


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: