Detailed description of the Data Interaction instance on the parent and child pages in JavaScript

Source: Internet
Author: User

In JS, if you want to publish the data on the parent or child page, you only need to use objective framesor directly use windows.prarent.doc ument. getElementById. Next, I will introduce it to you.

Writing about parent-child page data interaction in js

1. window. frames ["test" cmd.doc ument. getElementById ('menu ');
2. // Since all functions are stored in the window object, you can remove the window at the beginning:
3. frames ["test" mongo.doc ument. getElementById ('menu ');
4. // in the browser, the frame name attribute is equivalent to the window object of the Child page by default, so you can further abbreviated it:
5.test.doc ument. getElementById ('menu ');
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 you can further abbreviated it:
Test.doc ument. getElementById ('menu '); 2.2 accessing a child page function or object 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

1. // assume that child.html defines the showMesg function and needs to be called in the parent.
2. window. frames ['test']. showMesg ();
3. // abbreviated form
4. test. showMesg ();
5. // Similarly, the object is also accessed
6. alert (test. person );
// 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 );

Example

Parent window: a1.html

The Code is as follows: Copy code

<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> a.html </title>
 
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "this is my page">
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
</Head>
<Script language = "JavaScript">
Function openWin (){
Window. open (". /a2.html "," _ blank "," height = 200, width = 400, status = yes, toolbar = no, menubar = no, location = no ");
}
Function setValues (id, name ){
Document. getElementById ("cid"). value = id;
Document. getElementById ("cname"). value = name;
}
</Script>

<Body>
<Form name = "form1" action = "test.html" method = "post">
Customer id: <input type = "text" name = "cid" value = "" id = "cid"> <br>
Customer name <input type = "text" name = "cname" value = "" id = "cname">
<Input type = "button" name = "OK" value = "select customer" onclick = "openWin ();"/>
</Form>
</Body>

</Html>

Subwindow: a2.html

The Code is as follows: Copy code

<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> a2.html </title>
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "this is my page">
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
</Head>
<Script language = "JavaScript">
Function viewData (id, name ){
Window. opener. setValues (id, name );
Window. opener = null;
Window. close ();
}
</Script>
<Body>
<Table border = "1">
<Tr>
<Td> operation </td>
<Td> customer id </td>
<Td> customer name </td>
</Tr>

<Tr>
<Td> <input type = "button" value = "select" id = "ss" onclick = "viewData ('001', 'shenzhen Huawei ')"> </td>
<Td> 001 </td>
<Td> Shenzhen Huawei </td>
</Tr>
<Tr>
<Td> <input type = "button" value = "select" onclick = "viewData ('002 ', 'yonyou soft')"> </td>
<Td> 002 </td>
<Td> UFIDA software </td>
</Tr>
</Table>
</Body>
 


</Html>

Related Article

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.