Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. Window
  4. close()

Window: close() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

TheWindow.close() method closes the current window, orthe window on which it was called.

Windows arescript-closable if they were created by web content. This generally includes:

  • Windows opened usingWindow.open()
  • Windows opened via web content, such as links (<a>) or forms (<form>), without user modifier actions

Windows opened by browser UI actions — such as right-click → Open in new tab, Ctrl+Click, Shift+Click, or middle-click — are often not script-closable. They may only be closed if they have not been navigated (history length remains 1). Callingclose() otherwise typically shows a console warning:Scripts may not close windows that were not opened by script.

Note also thatclose() has no effect when called onWindowobjects returned byHTMLIFrameElement.contentWindow.

Syntax

js
close()

Parameters

None.

Return value

None (undefined).

Examples

Closing a window opened withwindow.open()

This example shows a method which opens a window and a second one which closes thewindow; this demonstrates how to useWindow.close() to close a windowopened by callingwindow.open().

js
// Global variable to store a reference to the opened windowlet openedWindow;function openWindow() {  openedWindow = window.open("more-info.htm");}function closeOpenedWindow() {  openedWindow.close();}

Specifications

Specification
HTML
# dom-window-close-dev

Browser compatibility

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp