This article mainly introduces the implementation of javascript parent-child page communication. The example analyzes the principle and related implementation skills of javascript for parent-child page communication, which has some reference value, for more information about how to implement javascript parent-child page communication, see the example in this article. Share it with you for your reference. The specific analysis is as follows:
If a page whose domain is www.abc.com contains an iframe whose name attribute value is childFrame, and the domain of this iframe is static.abc.com. You can set the domain of the parent page to abc.com, and the domain of the Child page to abc.com, and then implement parent-child page communication (here I am confused about the concept of parent-child page and cross-origin.
If the preceding method is not used, the Parent and Child pages can access each other.
On the parent page, use window. frames [0] or window. frames ["childFrame"] to return a Window object:
Var childWindow = window. frames [0]; // or window. frames ["childFrame"], or directly childFrame or childFrame. window var childDoc = childWindow. contentDocument | child1_1_doc ument;
ChildWindow can be used to access and execute sub-page defined functions, and childDoc can be used to access the DOM nodes of sub-pages.
To access the parent page on a child page, you can use parent (Window object). If a page is already a top-level page, parent = self will return true:
If (parent! = Self) {// the current page has a parent page // call the function parent of the parent page. parentFunc (); var parentDoc = parent. contentDocument | parent.doc ument; // DOM node for accessing the parent page}
Www.abc.com parent page:
Document. domain = 'abc. com '; var ifr = document. createElement ('iframe'); ifr. src = 'HTTP: // static.abc.com/'{ifr.style.display = 'none'; document. body. appendChild (ifr); ifr. onload = function () {var doc = ifr. contentDocument | ifr.content20.doc ument; // click here to control the subpage alert (doc. getElementsByTagName ("h1") [0]. childNodes [0]. nodeValue );};
Www.static.abc.com subpage:
The Code is as follows:
Document. domain = 'abc. com ';
I hope this article will help you design javascript programs.