Window close() method

Definition and usage

close() Method to close the window.

See also:

open()

Example

Example 1

Open a window using open() and close it using close():

let myWindow;
function openWin() {
  myWindow = window.open("", "myWindow", "width=200, height=100");
}
function closeWin() {
  myWindow.close();
}

Try it yourself

Example 2

Open "www.codew3c.com" in a new window and close it using close():

function openWin() {
  myWindow = window.open("", "_blank", "width=200, height=100");
}
function closeWin() {
  myWindow.close();
}

Try it yourself

Syntax

window.close()

Parameter

None.

Return value

None.

description

method close() will close the top-level browser window specified by window. self.close() or simply call close() to close itself.

Only windows opened by JavaScript code can be closed by JavaScript code. This prevents malicious scripts from terminating the user's browser.

Browser support

All browsers support close():

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support Support Support Support Support Support