JS: Open URL window.open(…)

By Xah Lee. Date: . Last updated: .

The window.open() method lets you open a new URL. Example:

click me to visit my JavaScript tutorial

Here's the code.

<p id="open42311">click me to visit my JavaScript tutorial</p>
function openIt () { open( "http://xahlee.info/js/js.html", "_blank"); }

document .getElementById("open42311").addEventListener( "click", openIt, false);

The window.open() property can be written just as open(). [see JS: Browser Window Object, DOM]

For how event works, see: DOM: Add/Remove Event Handler

window.open() Parameters

window.open() accepts 4 parameters, all optional. Only the first one is really necessary.

  1. URL (string)
  2. a window name (string).
  3. spec for window parameters. (string) Such as window size, position, show scrollbar, toolbar, etc.
  4. true or false. If true, whether to replace the current URL in history list.

The window name is a string, and some value has special meaning:

The “window name” concept and their special names is from old HTML spec (1990s), where a anchor <a> can have a name attribute. This “name” attribute is also used by frames:

Window Parameter Spec (size, position, toolbars)

The 3rd parameter to window.open() is a string. The purpose is to specify the newly opened window's position, size, whether to show toolbars, url field, etc. It is rarely used today. Most browsers will block popup anyway. There are many implicit restrictions by different browsers because spammer abused this feature to make it looks identical to a system alert (such as out of memory), and trick people to click, resulting visiting ad sites or malware sites.

Here's a example using the parameter:

function popup () { open( "http://xahlee.info/js/js.html",
 "width=640,height=480,menubar=no,scrollbar=no"); }
document .getElementById("popup44429"). addEventListener( "click", popup, false);

click me to see

For a complete list, see: https://developer.mozilla.org/en-US/docs/Web/API/Window.open

window.open() Return Value

window.open() return a new window object.

Liket it? I spend 2 years writing this tutorial. Help me spread it. Tell your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Object Reference
  5. DOM Scripting
  6. SVG
  7. Blog