How to close a browser window with HTML

Updated: 04/30/2020 by Computer Hope
Close window

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

Note

As of 2024, there is no way to close a web browser tab or window using JavaScript. Modern security standards do not permit this behavior. The JavaScript code below does not work anymore and is purely for educational purposes.

Closing a window with JavaScript: two former methods

The first method created a button that, when clicked, prompted the user "Do you want to close this window?" If the user clicked OK, the browser tab or window closed.

<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 code was inserted in the head of an HTML (HyperText Markup Language) page.

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

Then, the following line of code was inserted in the body of the HTML page.

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