HTML Close Window Code

You can use the following HTML code to close a window within your website. This can be handy on popup windows. To be precise, this is actually JavaScript code combined with HTML code.

Close Current Window

The following code closes the current window.

<a href="JavaScript:window.close()">Close</a>

To see this code in action, click the following button to open a popup page, then close it using its Close link:

(Popup window code here).

Close Another Window

You can also close another window. That is, a different window to the one that the "Close" button is located on.

The following JavaScript code includes a function to open the window, and another function to close the window that was just opened. To close the window, we use the window's name (the name that we gave it when we opened the window). In this case, we called our window popupWindow.

<script>
// Popup window code
function newWindow(url) {
	popupWindow = window.open(
		url,
		'popUpWindow','height=300,width=450,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
}

function closeWindow()
{
   if(false == popupWindow.closed)
   {
      popupWindow.close ();
   }
   else
   {
      alert('That window is already closed. Open the window first and try again!');
   }
}
</script>
<button onClick="JavaScript:newWindow('/javascript/examples/sample_popup.cfm')">Open Popup Window</button>
<button onClick="JavaScript:closeWindow();">Close Popup Window</button>

Note that you may need to click "Close Popup Window" twice - the first click will bring this window back into focus and the second click will click the actual button.