Example of a javascript parent-child page communication _ javascript skills

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.