Window.close()

Window.close() 方法关闭当前窗口或某个指定的窗口。

该方法只能由 Window.open() 方法打开的窗口的 window 对象来调用。如果一个窗口不是由脚本打开的,那么,在调用该方法时,JavaScript 控制台会出现类似下面的错误:不能使用脚本关闭一个不是由脚本打开的窗口。Scripts may not close windows that were not opened by script.

同时也要注意,对于由 HTMLIFrame​Element​.content​Window 返回的 Window 对象,close() 也没有效果。

语法

window.close();

例子

关闭一个由 window.open()方法打开的窗口

这个例子展示了如何使用这个方法关闭使用 window.open() 打开的窗口

// 用于存储将要打开的窗口(的引用)的全局变量
var openedWindow;

function openWindow() {
  openedWindow = window.open('moreinfo.htm');
}

function closeOpenedWindow() {
  openedWindow.close();
}

关闭当前窗口

当直接调用 window 对象的 close() 方法而非在一个 window 实例上调用 close() 时,浏览器会关闭最前面的窗口,无论是不是你的脚本创建的这个窗口。(Firefox 35.0.1:脚本不能关闭不是他打开的窗口)

function closeCurrentWindow() {
  window.close();
}

规范

Specification Status Comment
HTML Living Standard
window.close()
Living Standard
HTML5
Window.close()
Recommendation

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
closeChrome Full support 1Edge Full support 12Firefox Full support Yes
Notes
Full support Yes
Notes
Notes Starting in Firefox 46.0.1, Window.close() can no longer close windows that weren't opened by the same script. This is a security precaution.
IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support 18Firefox Android Full support Yes
Notes
Full support Yes
Notes
Notes Starting in Firefox 46.0.1, Window.close() can no longer close windows that weren't opened by the same script. This is a security precaution.
Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support
See implementation notes.
See implementation notes.