Please take a second out to visit our sponsors. Thanks.

Home / JavaScript Tutorials / Windows and JavaScript



JK Help Forum
Having trouble with anything? Visit our help forum to get the answers you need.

Want to print this page? Use this version instead.

Jump to...
-Tutorial Index
-Free JavaScripts
-Free Java Applets
-Web Tutorials
-Frontpage Tutorials

Low cost, high quality web hosting!
Click here for low cost, high quality web hosting!

Link to us!
We always welcome and appreciate links back to JavaScript Kit. Click here for details.


Windows and JavaScript (one part)

Using JavaScript, we can manipulate windows-open them, close them, reload them, load another page into them-just name it.

Red_CurlyC035.gif (285 bytes) Opening a new window using JavaScript

With JavaScript, you can easily manipulate windows. One of the most asked questions when it comes to windows is how to open more than one window. Ok, then, lets first talk about that. To open a window, simply use the open() method of JavaScript. ("URL")

Lets say you just want to open a window containing "page2.htm"

<input type="button" value="Click here to see">

Red_CurlyC035.gif (285 bytes) Hiding/showing toolbars, menu bars, status bus etc upon opening a window

Lets talk about some of the attributes we can add to the above script to control not only the size of the opened window, but also, what is to be shown: toolbar, menu bar etc. To add attributes when a window is opened, we still would use the method, but with a little addition:


As you can see, we first have to add in a name (this name has nothing to do with adding attributes-it is the window name used in the TARGET attribute of a <a> and <form> tag-don't worry about it, it is not commonly used). Moving on, here are the complete list of attributes you can add:

width height toolbar
location directories status
scrollbars resizable menubar

Here is an example that opens a window that's not only smaller in size, but with ONLY the menubar turned on:

<input type="button" value="Click here to see">

Here is another example with no attributes turned on, except the size changed:

<input type="button" value="Click here to see">

Play around with them, if you like, but I'll end it here.

Red_CurlyC035.gif (285 bytes) Reloading, closing, loading new content into a window using JavaScript

With JavaScript, we can write code that replaces the "reload" and "close" button of our browser.

To reload a window, use this method:


Here's an example:

To close a window, use this method:


I was almost tempted to provide an example!

Ok, we can also load new content into a window using JavaScript-this is similar to the <a> tag in html, only now, using JavaScript, we have more control over this process.

The basic syntax when loading new content into a window is:


This is exactly the same as

<a href="></a> //if we use html

Lets provide an example, where a confirm box will allow users to choose between going to two places:

function warp()
var ok=confirm('Click "OK" to go to geocities, "CANCEL" to go to CNN')
if (ok)

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=

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:

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

//for button

//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.

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

windows object
properties methods

End of Tutorial
CopyRight 1997, 1998 JavaScript Kit. NO PART may be reproduced without author's permission.