目录

window open()

示例1

在新的浏览器标签中打开"www.91xjr.com":

window.open("https://www.91xjr.com");
亲自试一试 »

下面有更多示例。


描述

这个open()方法会打开一个新的浏览器窗口或新选项卡,具体取决于您的浏览器设置和参数值。

也可以看看:

close() 方法.


语法

window.open( URL, name, specs, replace)

参数

Parameter Description
URL Optional.
The URL of the page to open.
If no URL is specified, a new blank window/tab is opened
name Optional.
The target attribute or the name of the window.
The following values are supported:
Value Description
_blank URL is loaded into a new window, or tab. This is the default
_parent URL is loaded into the parent frame
_self URL replaces the current page
_top URL replaces any framesets that may be loaded
name The name of the window (does not specify the title of the window)
specs Optional.
A comma-separated list of items, no whitespaces.
The following values are supported:
fullscreen=yes|no|1|0 Whether or not to display the browser in full-screen mode. Default is no. A window in full-screen mode must also be in theater mode. IE only
height=pixels The height of the window. Min. value is 100
left=pixels The left position of the window. Negative values not allowed
location=yes|no|1|0 Whether or not to display the address field. Opera only
menubar=yes|no|1|0 Whether or not to display the menu bar
resizable=yes|no|1|0 Whether or not the window is resizable. IE only
scrollbars=yes|no|1|0 Whether or not to display scroll bars. IE, Firefox & Opera only
status=yes|no|1|0 Whether or not to add a status bar
titlebar=yes|no|1|0 Whether or not to display the title bar. Ignored unless the calling application is an HTML Application or a trusted dialog box
toolbar=yes|no|1|0 Whether or not to display the browser toolbar. IE and Firefox only
top=pixels The top position of the window. Negative values not allowed
width=pixels The width of the window. Min. value is 100
replace

Deprecated

Specifies whether the URL creates a new entry or replaces the current entry in the history list. The following values are supported:
  • true - URL replaces the current document in the history list
  • false - URL creates a new entry in the history list

Warning

Chrome throws an exception when using this parameter.

Source: Bugs Chromium Issue 1164959.

返回值

对新窗口的引用,或无效的如果通话失败。


更多示例

在新窗口/选项卡中打开“关于:空白”页面:

var myWindow = window.open("", "", "width=200,height=100");
亲自试一试 »

打开一个名为 "MsgWindow" 的新窗口,然后在其中写入一些文本:

var myWindow = window.open("", "MsgWindow", "width=200,height=100");
myWindow.document.write("<p>This is 'MsgWindow'. I am 200px wide and 100px tall!</p>");
亲自试一试 »

用新窗口替换当前窗口:

var myWindow = window.open("", "_self");
myWindow.document.write("<p>I replaced the current window.</p>");
亲自试一试 »

打开一个新窗口并控制其外观:

window.open("https://www.91xjr.com", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
亲自试一试 »

打开多个选项卡:

window.open(" http://www.google.com/");
window.open("https://www.91xjr.com/");
亲自试一试 »

打开一个新窗口。使用 close() 关闭新窗口:

function openWin() {
  myWindow = window.open("", "myWindow", "width=200,height=100");   // Opens a new window
}

function closeWin() {
  myWindow.close();   // Closes the new window
}
亲自试一试 »

打开一个新窗口。使用 name 属性返回新窗口的名称:

var myWindow = window.open("", "MsgWindow", "width=200,height=100");
myWindow.document.write("<p>This window's name is: " + myWindow.name + "</p>");
亲自试一试 »

使用 opener 属性返回对创建新窗口的窗口的引用:

var myWindow = window.open("", "myWindow", "width=200,height=100");   // Opens a new window
myWindow.document.write("<p>This is 'myWindow'</p>");   // Text in the new window
myWindow.opener.document.write("<p>This is the source window!</p>");  // Text in the window that created the new window
亲自试一试 »

浏览器支持

open()所有浏览器都支持:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes