The close()
method closes the current window.
close |
Yes | Yes | Yes | Yes | Yes |
window.close()
None.
No return value.
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:
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: