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:
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.
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.
You can see a complete description of the window object and what's possible.
- Tutorial introduction
- Customizing the look of the new window
- Accessing objects of a window via another