Using the window.close method

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.

Comments on this entry are closed.

  • Really awesome blog. Thanks for sharing this informative blog. Currently I have completed Software Testing Training in Chennai at a reputed training institutes. If you are looking for best Software Testing Training Institutes in Chennai reach FITA located at Chennai, India. Rated as No.1 placement and training center in Chennai. For more details visit this site.

  • Your blog is really useful for me.


    Oracle Training in Chennai

  • Your blog is really useful for me. Thanks for sharing this informative blog. If anyone wants to get real time Oracle Training in Chennai reach FITA Oracle Training Institutes in Chennai. They give professional and job oriented training for all students.

  • Thanks for sharing this informative blog. If anyone want to get ios training, android training in Chennai please refer this link. Android Training Institute in Chennai, Android Training in Chennai

  • I get a lot of great information from this blog. Thank you for your sharing this informative blog. I have bookmarked this page for my future reference. Just now I have completed hadoop certification course at a leading academy. If you are looking for best Hadoop Training in Chennai visit FITA IT training and placement academy.

  • Thanks for that.very useful information…hadoop training chennai

  • Thank you very much. Saved me a lot of time.

  • Its very helpful……i gt idea…

  • Thanks you did a great job.

  • Nice blog… Thanks

  • both not working at all.!!

  • I try “javascript:self.close();” or “javascript:windows.close();” also doesn’t work

  • Good blog, visit my blog too.
    You can find some useful java and javascript programs

  • Nice post dude.

  • javascript:self.close(); I searched this on google and came here, a service report center used it to close a popup and Mozilla showed me what JavaScript is used to do it lol!

  • i tried self.close(); but its not working

  • oops.. so here it is:
    put this onclick=”window.close();” in an “a” tag.. 😉

  • it’s “javascript:self.close();”

  • i have tried self.close (); but it doesn’t work

  • thansk so much ! i am using popup(width,height,name) and do not know how to close it ! 😀 funny me ! 😀