Summary of javascript parent-child page Interaction

Source: Internet
Author: User

 

Frames are used to store sub-pages, either iframe or frameset. The window object is a global object, and all functions and objects on the page are in its scope.

1. parent indicates the parent window. If the parent window has several layers of nesting, top indicates the top-level parent window. Self indicates the window itself.

Java code

If (self = top) {//} checks whether the window is in the top level

If (self = parent) {} // You can also

2. Parent-Child page Interaction

1) access child page elements from the parent page. The elements of the Child page are all in their own temporary Doc ument object. Get it first and then let's talk about it.

It is best to set the name attribute for frames, which is the most convenient operation. For example

Java code

<Iframe name = "test" src = "child.html"> </iframe>

If you want to obtain the element whose id is 'menu 'in child.html, you can write it like this:

Java code

Window. frames ["test" cmd.doc ument. getElementById ('menu ');

// Because all functions are stored in the window object, you can remove the window at the beginning:

Frames ["test" cmd.doc ument. getElementById ('menu ');

In the browser, the frame name attribute is equivalent to the window object of the Child page by default, so it can be further abbreviated:

Test.doc ument. getElementById ('menu ');

2) access sub-page functions or objects on the parent page. The function and object of the subpage are in the window object. The key is to obtain the object.

Java code

// If child.html defines the showMesg function and needs to be called in the parent, write

Window. frames ['test']. showMesg ();

// Abbreviated form

Test. showMesg ();

// Similarly, the object is also accessed

Alert (test. person );

3) Other methods to obtain the document.

Use 'document. getElementById () 'or 'document. getElementsByTagName () 'obtains the frame as an Element under the document and then accesses its attributes contentDocument/contentWindow (exclusive to iframe and frame). The first ie7 is not supported, the second chrome is not supported.

Java code

<Iframe id = "testId" src = "child.html"> </iframe>

// ======

Var doc = document. getElementById ('testid ');

// Or

Var doc = document. getElementsByTagName ('iframe') [0];

Then

Var winOrdoc = doc. contentDocument | doc. contentWindow; // select either.

If(winOrdoc.document)winOrdoc=winOrdoc.doc ument;

WinOrdoc. getElementById ('menu ');

4) access the parent page element from the Child page. Thinking is the same as 1st, first get the parent window named Doc ument object.

Java code

Parent.w.w.doc ument. getElementById ('parentmenu ');

// Shorthand

Parent.doc ument. getElementById ('parentmenu ');

5) access the function or object of the parent page on the Child page. The idea is the same as 2.2. first obtain the parent window object.

Java code

Parent. parentFunction ();

 

From progress in crawling

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.