Friday, June 25, 2010

Open a new browser window using javascript window.open() method

This article is to explain opening a new browser window using JavaScript. The window.open() function of JavaScript has been explained in detail with example code. For further help don't hesitate to contact me or our friends on the network. Just post your queries on the comment section.


Syntax:
winRef = window.open( URL, name [ , features [, replace ] ] )
Example:
mySearchWin = window.open('http://www.google.com','mysearchwindow',
'left=30,top=30,width=600,height=400,toolbar=0,resizable=0')
Returns:
Reference to the newly opened window.

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 URL, name, features, replace have the following meaning:

URL 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 as the URL (this may be the case when you are going to write some script-generated content to your new window).
name 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 name ...>. For example, you can use hyperlinks of the form name 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.
features An optional string parameter specifying the features of the new window. The features string may contain one or more feature=value pairs separated by commas.
replace 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 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.
top=pixels Specifies the Y coordinate of the top left corner of the new window. (Not supported in version 3 browsers.)
left=pixels Specifies the X coordinate of the top left corner of the new window. (Not supported in version 3 browsers.)