Home / JavaScript Tutorials / Windows and JavaScript


 

CodingForums
Having trouble with scripting? Visit our help forum to get the answers you need.

Jump to...
-Free JavaScripts
-JS tutorials
-JS Reference
-DHTML/CSS

-Free applets
-Web Tutorials
-Link to Us!

- CSS Drive
- JavaScript Menus
- PHP Resources
- Hotels & Vacations
- Java Online Casinos
- Web hosting Search


Red_CurlyC035.gif (285 bytes) 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 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

-Tutorial introduction
-opening a new window using JavaScript
-Hiding/showing toolbars, menu bars, status bus etc upon opening a window
-reloading, closing, loading new content into a window using JavaScript
-Accessing objects of a window via another


End of tutorial

http://javascriptkit.com
Copyright © 1997-2005 JavaScript Kit. NO PART may be reproduced without author's permission.