Opening a window with JavaScript

JavaScript FAQ | JavaScript Windows FAQ  

Question: How do I open a new browser window?

Answer: To open a new browser window, use the window.open() method. For example, the following code opens this page in a new window.

myRef = window.open(''+self.location,'mywin',
'left=20,top=20,width=500,height=500,toolbar=1,resizable=0');

The general syntax of the window.open() method is as follows:

winRef = window.open( sURL, sName [ , sFeatures [, bReplace ] ] )
The return value, stored in the variable winRef, is the reference to your new window. You can use this reference later, for example, to close this window (winRef.close()), give focus to the window (winRef.focus()) or perform other window manipulations.

The parameters sURL, sName, sFeatures, bReplace have the following meaning:
sURL String specifying the location of the Web page to be displayed in the new window. If you do not want to specify the location, pass an empty string in sURL (this may be the case when you write some script-generated content to your new window).
sName String specifying the name of the new window. This name can be used in the same constructions as the frame name provided in the frame tag within a frameset <FRAME NAME=sName ...>. For example, you can use hyperlinks of the form <a target=sName href="page.htm">, and the hyperlink destination page will be displayed in your new window.

If a window with this name already exists, then window.open() will display the new content in that existing window, rather than creating a new one.

sFeatures  An optional string parameter specifying the features of the new window. The sFeatures string may contain one or more feature=value pairs separated by commas.
bReplace An optional boolean parameter. If true, the new location will replace the current page in the browser's navigation history. Note that some browsers will simply ignore this parameter.

The following features (set in the sFeatures parameter) are available in most browsers:
toolbar=0|1 Specifies whether to display the toolbar in the new window.
location=0|1 Specifies whether to display the address line in the new window.
directories=0|1 Specifies whether to display the Netscape directory buttons.
status=0|1 Specifies whether to display the browser status bar.
menubar=0|1 Specifies whether to display the browser menu bar.
scrollbars=0|1 Specifies whether the new window should have scrollbars.
resizable=0|1 Specifies whether the new window is resizable.
width=pixels Specifies the width of the new window.
height=pixels Specifies the height of the new window.
left=pixels Specifies the X coordinate of the top left corner of the new window.
top=pixels Specifies the Y coordinate of the top left corner of the new window.

Copyright © 1999-2012, JavaScripter.net.