Accessing objects of a window via another

After you open a secondary window, there is a connection between the original window and this newly opened one that allows you to cross-access objects/variables, properties of each window. But before we discuss how this is done, we need to first discuss window names, a very different kind from the target name mentioned in the previous page. Look at below:

var hello=open('page2.htm','hi','width=200,height=200')

By assigning this window to a variable upon opening it, we create a connection to this window from the window that opened it. Whenever we want to access anything that's inside this newly opened window, for example, to write to this window, we would do this: hello.document.write("hi!") inside the current window.

Ok, lets say you want to, from the current window, open a secondary window, and FROM HERE, change the background color of this newly opened window.

Then, click the below buttons:

Change background color:

The radio buttons are here, but we have changed the bgcolor of another window...lets see the core code:

//for button
onClick="win1=open('page2.htm','winname', 'width=200,height=200')"

//for radio button3
//for radio button2
//for radio button3

The most important part is: win1.document.bgColor, which is what caused the bgcolor to change in the secondary window, instead of the one that contains the script. Also notice that we used another method, focus(), to bring focus to the second window every time it changes.

You can see a complete description of the window object and what's possible.

Copyright 1997-2014 JavaScript Kit. NO PART may be reproduced without author's permission.