How do I close a browser window with HTML code?

Updated: 07/25/2017 by Computer Hope

Close windowAs of 2017, there is no way to close a web browser tab or window using JavaScript. Modern security standards do not permit this behavior.

For historical reasons, this page lists methods that were formerly used to programmatically close a browser tab or window using JavaScript.

Closing a window with JavaScript: two former methods

The first method created a button that, when clicked, prompts the user "Do you want to close this window?" If the user clicked OK, the window would close.

<input type="button" value="Close this window" onclick="self.close()">

The second method required a bit more work, but avoided the "prompting issue". The following lines of code were to be inserted into the head of an HTML document:

<script language="javascript" type="text/javascript">
function windowClose() {
window.open('','_parent','');
window.close();
}
</script>

Then, the following line of code was to be inserted into the body of the HTML document:

<input type="button" value="Close this window" onclick="windowClose();">

Additional information

  • See our close and window definitions for further information and related links on these term.