JS child window to the parent window to pass value

Source: Internet
Author: User

First, let's look at the method of the next Window object open:

The 1,open () method is used to open a new browse

window.open (Url,name,features,replace)

Important : Please do not confuse method window.open () with Method Document.open (), which has a completely different function. To make your code clear, use window.open () instead of open ().

2,opener

The opener property is a readable, writable property that returns a reference to the Window object that created it. ---How to understand this sentence, for example

Opener that who open my, such as a page with window.open pop up the B page window, then a page is located in the window is B page opener, b page through the opener object can access a page.

Note: You must use the Window.Open method to use the Window.opener callback value when the parent window is required to open a child window.

   Window.opener.document.getElementById ("Downloads"). Value = ' new value ';

Attach an example of a child window to the parent window for passing values

1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4     <MetaCharSet= "UTF-8">5     <title>JS implements child window to pass value to parent window--parent page</title>6 </Head>7 <Body>8 <!--The Open () method opens a new browser window or looks for a named window.  -9 <inputtype= "button"value= "Open child window"onclick= "Open_win" (' children.html ', ' children ', ' + ') '/>Ten  One <inputvalue= "333"ID= "Downloads" /> A  - <Script> -     functionopen_win (URL, title, W, h) { the         variwidth=W; -         variheight=h; -         varITop=(Window.screen.availHeight- --iheight)/p; //get the vertical position of the window; -         varILeft=(Window.screen.availWidth-Ten-iwidth)/p; //get the horizontal position of the window; + Mywindow=window.open (URL, title,'height=' +iheight+ ',, innerheight=' +iheight+ ', Width=' +iwidth+ ', Innerwidth=' +iwidth+ ', top=' +ITop+ ', left=' +ILeft+ ', Toolbar=no, Menubar=no, Scrollbars=no, Resizable=no, Location=no, Status=no, Titlebar=yes , AlwaysRaised=yes'); -     } +  A </Script> at </Body> - </HTML>
<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title>JS implements the child window to pass the value to the parent window--child page</title></Head><Body>I am the child window<Script>window.onload= function() {window.opener.document.getElementById ("Downloads"). Value= 'New Value'; }</Script></Body></HTML>

Note: These two files can no longer be opened directly locally, will be an error: must be set up on the server

Children.html:11 uncaught domexception:blocked A frame with origin ' null ' from accessing a cross-origin frame.

The opener property is useful for creating a window that can reference properties and functions defined by the window that created it.

Window.opener

Note: Only the Operner property of the window object that represents the top-level window is valid, and the Operner property of the Window object that represents the frame is invalid.

3, then talk about the difference between opener and the parent and attach top

Window.parent is an IFRAME page called the parent Page object, when we want to access the outer page from the IFRAME embedded page can be directly used window.parent get, Window.opener is window.open or hyperlink <a > Calling the parent Page object on a child page that is open

The parent represents the parents window, which is used to access the parent page's objects in the child pages generated in Iframe,frame. For example, a page with an IFRAME or frame to invoke B page, then a page is located in the window is the parent of page B.

The top property returns the topmost ancestor window.

This property returns a read-only reference to a top-level window. If the window itself is a top-level window, the top property holds a reference to the window itself. If the window is a frame, then the top property references the top-level window that contains the frame.

Window.top

Can be applied to the frame jumping out of frames

function breakout ()  {  if (window.top!=window.self)     {    window.top.location= "/example/hdom/tryjs_ Breakout.htm "    }  }</script>

  Attached: Hierarchy Relationship of Window object, parent object, frame object, Document object, and Form object
The Window object →parent object →frame Object →document object →form object,
as follows: Parent.frame1.document.forms[0].elements[0].value;

4, modal window communicates with parent window

A child window created by using Showmodeldialog (), and the showModelessDialog () method cannot communicate with the parent window by Window.opener

To get the parent window object. To implement communication, you must pass in the parent window object to the child window when you create the modal child window.

JS child window to the parent window to pass value

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.