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

Displaying a Page in a Window

Opening and closing browser windows are all operations which are simple to implement with JavaScript. Let's start with the basics and see how to open a window where you define its size and the page to display.

The corresponding code could not be simpler:

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

Four parameters are used to adapt the window to the content it receives:

  • Destination_URL: The address of the page to be displayed in the new window.

  • This parameter is optional as you could open a window and not display anything in it at all. But what would you want to do that for? For writing content dynamically ... as you will see later.

  • Window_Name: The name assigned to the window.

  • This parameter is also optional. It is used to control the window (to change its content, to close it, to move it, etc.) from another window. We will also look at this later on ... be patient!

  • width and height : in pixels!

  • These parameters are self-explanatory. In the case of a window displaying an image, you can obtain its size information via a tool such as Paint Shop Pro. Note that you cannot open a window with dimensions which are smaller than 100 x 100 pixels.

Did you say optional? A parameter which is optional does not have to be specified but you still have to consider it in the argument list. For instance, if you don't want to give either a URL or a name, specify an empty string for each of the corresponding arguments ('' - two consecutive single quotes):

window.open('','','width=200,height=200');