Window close() 方法
实例
使用 open() 打开新窗口,使用 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>This is 'myWindow'</p>");}function closeWin() {myWindow.close();}</script></body></html>
定义与用法
close() 方法关闭当前窗口。
提示:此方法通常与 open() 方法一起使用。
浏览器支持
| 方法 | |||||
|---|---|---|---|---|---|
| close() | Yes | Yes | Yes | Yes | Yes |
语法
window.close()
参数
| 无 |
技术细节
| 返回值: | 无返回值 |
|---|
更多实例
实例
Open "cankaoshouce.com" in a new window, and use close() to close the window:
在新窗口中打开 "cankaoshouce.com" ,然后使用 close() 关闭窗口
<!DOCTYPE html><html><body><button onclick="openWin()">Open cankaoshouce.com in a new window</button><button onclick="closeWin()">Close the new window (cankaoshouce.com)</button><script>var myWindow;function openWin() {myWindow = window.open("https://cankaoshouce.com", "_blank", "width=500, height=500");}function closeWin() {myWindow.close();}</script></body></html>