How do I close a browser window with HTML code?

Disclaimer:There is not a method that works with the current version of 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, it prompts the user, asking "Do you want to close this window?"

Instruction: Insert the following line of code into the body of your 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 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 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.