Accessing objects of a window via another

You are here:

After you open a second 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 one mentioned in the beginning of this section.

Look at below:

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

By giving this window a name using the above method, for example, "hello", it will give you access to anything that's inside this window from other windows. 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!")
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.

Now, click the below buttons:

Change background color: ...lightgreen:...lightyellow:...pink:

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
onClick=
"win1.document.bgColor=
'lightgreen';win1.focus()"

//for radio button2
onClick=
"win1.document.bgColor=
'lightyellow';win1.focus()"

//for radio button3
onClick=
"win1.document.bgColor=
'pink';win1.focus()"

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.

Here's the complete listing of the window object we touched upon in this tutorial:

 

windows object
properties methods
closed
defaultStatus
document
Frame
frames
history
length
location
name
opener
parent
alert
blur
clearTimeOut
close
confirm
focus
open
prompt
setTimeOut


 

 

Sandra's Page Home is a personal and experimental site on the McIntyre Network
 at
http://www.mcintyre-network.com. Original and adapted material from credited sources.