The difference and use of window.parent and Window.opener

Source: Internet
Author: User

1, Window.parent is the IFRAME page called the parent Page object

Example: a.html

<body>
<form name= "Form1" Id= "Form1" >
<input type= "text" name= "username" id= "username"/>
</form>
<iframe src= "b.html" width=100%></iframe>
</body>

If we need to assign a value to the Username text box in the a.html (like many upload functions, upload function page in the Ifrmae, upload the path after uploading the b.html in the parent page text box), we should write in the b.html:

<script type= "Text/javascript" >
var _parentwin = window.parent;
_parentwin.form1.username.value = "XXXX";
</script>

Z-blog's article editing page upload function is so realized.

2. Window.opener is the parent page object called by window.open open child page

Opener: A reference to the Window object that opens the current window whose value is null if the current window is opened by the user.

Self represents its own window, and opener represents the window that opens itself, such as the Window a.html open window b.html. If you rely on the Window.Open method, the window b.html,self represents b.html itself, and the opener represents the window a.html.

Example: a.html

<input type= "text" name= "username" id= "username"/>
<a onclick= "window.open (This.href, ' ', ' resizable=yes,width=800,height=600,status '); return false "href=" b.html ">B</a>

If you need to assign a value to a FORM element in a.html in b.html, we should write this in b.html

<a href= "Javascript:try{window.opener.document.getelementbyid (' username '). Contentwindow.
Frames[0].document.getelementsbytagname (' body ') [0].innerhtml+= ' xxx '}catch (e) {};window.close (); " > Insert </a>

The

Closes the b.html with Window.close later. Windsphoto 2.7.3  in the article edit page pop-up new window (Picture list), choose to insert the uploaded image is so implemented.

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.