14.8.1 Opening and Closing Windows

14.8.1 Opening and Closing Windows

You can open a new web browser window (or tab; this is usually a browser configuration option) with the open() method of the Window object. Window.open() loads a specified URL into a new or existing window and returns the Window object that represents that window. It takes four optional arguments:

The first argument to open()is the URL of the document to display in the new window. If this argument is omitted (or is the empty string), the special blank-page URL about:blank is used.

The second argument to open() is a string that specifies a window name. If a window by that name already exists (and if the script is allowed to navigate that window), that existing window is used. Otherwise a new window is created and is assigned the specified name. If this argument is omitted, the special name “_blank” is used: it opens a new, unnamed window.

Note that scripts cannot simply guess window names and take over the windows in use by other web applications: they can only name existing windows that they are “allowed to navigate” (the term is from the HTML5 specification). Loosely, a script can specify an existing window by name only if that window contains a document from the same origin or if the script opened that window (or recursively opened a window that opened that window). Also, if one window is a frame nested within the other, a script in either one can navigate the other. In this case, the reserved names “_top” (the top-level ancestor window) and “_parent” (the immediate parent window) can be useful.