JavaScript Coder

Using the window.close method

beginner javascript close window javascript window.close

It may be needed that you need to give a close link in the popup you created. The window.close () method could be used to close a window. However, there are certain security restrictions for using the close() method.

The close method closes only windows opened by JavaScript using the open method. If you attempt to close any other window, a confirm message is displayed, asking the user to choose whether the window is to be closed or not. If you have the reference to a window, you can use the reference to the window to close.

For example:

popup_window ="");


popup_window.close ();

You can close the current window using self.close (); For example:

<a href="self.close ()">Close this Window</a>

Sample Code for window.close()

   <title>JavaScript Window Close Example </title>
<script type="text/javascript">
   function popuponclick()
      my_window ="",

      my_window.document.write('<h1>The Popup Window</h1>');

   function closepopup()
      if(false == my_window.closed)
         my_window.close ();
         alert('Window already closed!');
      <a href="javascript: popuponclick()">Open Popup Window</a>

      <a href="javascript: closepopup()">Close the Popup Window</a>

See the code above in work in the Link below.

JavaScript Window Close Example 1

Click on the ‘Open Popup window’ link to open the popup first and then click on the ‘Close the Popup window’ link to close the popup.

See Also