JavaScript : Open, Close, and Modify Browser Windows
Contents ]
Dr Benton

Controlling One Window From Another

If you have followed our workshops, you will know that everything in a browser is an object. Windows are no exception. To control a window object, you will need to do more than simply using the name it receives when it is opened.

The new window must first be assigned to an object variable with the following syntax:

MyWindow = window.open('Destination_URL','Window_Name','width=width,height=height');

The object MyWindow can then be handled using JavaScript just like any other window object. You can change its position as well as its content. On the other hand, you might come across those irritating incompatibility problems between Internet Explorer and Navigator. For instance, Netscape will recognise the function moveTo for window positioning while Internet Explorer will show no reaction.

This means that the instruction:

<A HREF="javascript:void(0)" onClick="MyWindow.moveTo(10, 10);">Move the window</A>

will shift our window MyWindow to the screen coordinates (10,10), but only in Netscape!

Fortunately, the close method will work in both Microsoft and Netscape browsers.

<A HREF="javascript:void(0)" onClick="MyWindow = window.open('stracy.jpg','Spencer','width=190,height=210');" >Another window</A>
<BR>
<A HREF="javascript:void(0)" onClick="MyWindow.close();">Close the window</A>

Another window
Close the window

And what about the name? The name assigned to a window does not allow you to change window properties. However, it does enable you to display a page inside it without the need for JavaScript. Just define a TARGET tag in a <A HREF> tag.