Javascript Reference - Window close() Method








The close() method closes the current window.

Browser Support

close Yes Yes Yes Yes Yes

Syntax

window.close()

Parameters

None.

Return Value

No return value.

Example

The following code shows how to use open() to open a new window, and close() to close the new window.


<!DOCTYPE html>
<html>
<body>
<!-- w w w.j  ava2 s .  c  o m-->
<button onclick="openWin()">Open "myWindow"</button>
<button onclick="closeWin()">Close "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>

The code above is rendered as follows:





Example 2

The following code shows how to open "www.example.com" in a new window, and use close() to close the window.


<!DOCTYPE html>
<html>
<body>
<!--from   www .jav a  2  s.  c  om-->
<button onclick="openWin()">Open</button>
<button onclick="closeWin()">Close</button>

<script>
var myWindow;

function openWin() {
    myWindow = window.open("http://www.example.com", "_blank", "width=500, height=500");
}

function closeWin() {
    myWindow.close();
}
</script>

</body>
</html>

The code above is rendered as follows: