Window open() 方法
实例
在浏览器的新选项卡中打开 "cankaoshouce.com" :
<!DOCTYPE html><html><body><p>点击按钮打开一个新窗口</p><button onclick="myFunction()">试一试</button><script>function myFunction() {window.open("https://cankaoshouce.com");}</script></body></html>
定义与用法
open() 方法打开新的浏览器窗口或新选项卡,具体取决于浏览器设置和参数值。
提示:使用 close() 方法关闭窗口。
浏览器支持
| 方法 | |||||
|---|---|---|---|---|---|
| open() | Yes | Yes | Yes | Yes | Yes |
语法
window.open(URL, name, specs, replace)
参数值
| 参数 | 描述 | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| URL | 可选。指定要打开的页面的 URL。如果未指定 URL,将打开一个新的窗口/选项卡,其中包含 about:blank | ||||||||||||||||||||||||||||
| name | 可选。指定窗口的目标属性或名称。支持以下值:
| ||||||||||||||||||||||||||||
| specs | 可选。 以逗号分隔的项目列表,无空格。支持以下值:
| ||||||||||||||||||||||||||||
| replace | Deprecated指定 URL 是创建新条目还是替换历史记录列表中的当前条目。支持以下值:
WarningChrome 在使用此参数时引发异常 |
技术细节
| 返回值: | 对新创建的窗口的引用,如果调用失败,则为 null |
|---|
更多实例
实例
在新窗口/选项卡中打开空白页 about:blank:
<!DOCTYPE html><html><body><p>单击该按钮可在宽 200px、高 100px 的新浏览器窗口中打开空白页面 about:blank。</p><button onclick="myFunction()">试一试</button><script>function myFunction() {var myWindow = window.open("", "", "width=200,height=100");}</script></body></html>
实例
在新窗口/选项卡中打开空白页 about:blank, 然后写一些文本进去:
<!DOCTYPE html><html><body><p>单击按钮打开一个名为 "MsgWindow" 的新窗口,其中包含一些文本。</p><button onclick="myFunction()">试一试</button><script>function myFunction() {var myWindow = window.open("", "MsgWindow", "width=200,height=100");myWindow.document.write("<p>这是 'MsgWindow'. 200px 宽,100px 高!</p>");}</script></body></html>
实例
用新窗口替换当前窗口:
<!DOCTYPE html><html><body><p>单击按钮将新窗口放入当前窗口。</p><button onclick="myFunction()">试一试</button><script>function myFunction() {var myWindow = window.open("", "_self");myWindow.document.write("<p>替换了当前窗口。</p>");}</script></body></html>
实例
打开新窗口并控制其外观:
<!DOCTYPE html><html><body><p>单击该按钮可打开具有某些设置的新窗口。</p><button onclick="myFunction()">试一试</button><script>function myFunction() {window.open("https://cankaoshouce.com", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");}</script></body></html>
实例
打开大量窗口选项卡:
<!DOCTYPE html><html><body><p>单击该按钮可打开多个选项卡。</p><button onclick="myFunction()">打开窗口</button><script>function myFunction() {window.open("http://www.google.com/");window.open("https://www.baidu.com/");window.open("https://cankaoshouce.com/");}</script></body></html>
实例
打开一个新窗口。使用 close() 关闭新窗口:
<!DOCTYPE html><html><body><button onclick="openWin()">打开 "myWindow"</button><button onclick="closeWin()">关闭 "myWindow"</button><script>var myWindow;function openWin() {myWindow = window.open("", "myWindow", "width=200,height=100");myWindow.document.write("<p>这是 'myWindow'</p>");}function closeWin() {myWindow.close();}</script></body></html>
实例
打开一个新窗口。使用 name 属性返回新窗口的名称:
<!DOCTYPE html><html><body><p>单击按钮创建一个窗口,然后显示新窗口的名称。</p><button onclick="myFunction()">试一试</button><script>function myFunction() {var myWindow = window.open("", "MsgWindow", "width=200,height=100");myWindow.document.write("<p>这个窗体的名字是: " + myWindow.name + "</p>");}</script></body></html>
实例
使用 opener 属性返回对创建新窗体的引用:
<!DOCTYPE html><html><body><p>单击按钮在新窗体和原(父)窗口中写入一些文本。</p><button onclick="myFunction()">试一试</button><script>function myFunction() {var myWindow = window.open("", "myWindow", "width=200,height=100");myWindow.document.write("<p>这是 'myWindow'</p>");myWindow.opener.document.write("<p>这是原窗体!</p>");}</script></body></html>