How do I close a browser window with HTML code?

Disclaimer:The code and methods on this page only work with Internet Explorer. There is no code and method that works with the latest versions of Google Chrome or Mozilla Firefox due to the browser's programming.

Without JavaScript

This method is nice because it goes straight into the source code without the need of additional JavaScript, but it does have a drawback. When the button is clicked in Internet Explorer, a "Do you want to close this window?" prompts appears, requiring the user to click the OK button to close the window.

Instructions: Insert the following line of code into the body of your HTML page.

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

Result:

With JavaScript

This method requires a bit more work, but avoids the "prompting issue" discussed in the previous section.

Step One: Insert the following lines of code into the head of your HTML page.

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

Step Two: Insert the following line of code into the body of your HTML page.

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

Result:

Additional information

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