You are here: Home » All Posts » JavaScript Popup Windows » Using the window.close method

Using the window.close method

in JavaScript Popup Windows

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 = window.open("");

.....

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()

<html>
<head>
   <title>JavaScript Window Close Example </title>
</head>
<script type="text/javascript">
   function popuponclick()
   {
      my_window = window.open("",
       "mywindow","status=1,width=350,height=150");

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

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

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

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.

Be Sociable, Share!
Reach out:

Prasanth Janardanan

Prasanth is the founder of Simfatic Solutions. He loves exploring Javascript possibilities and shares some great tips and some code here at Javascript-Coder.com.
Most of the time, Prasanth is busy adding features to Simfatic Forms - a desktop based Web Form Builder.
Reach out:

Latest posts by Prasanth Janardanan (see all)

{ 12 comments… read them below or add one }

Mukesh May 12, 2013 at 7:12 pm

Thanks you did a great job. http://www.bloggerbonus.com/

Reply

sum January 18, 2012 at 10:14 am

Nice blog… Thanks

Reply

Rupesh November 16, 2011 at 9:45 am

both not working at all.!!

Reply

test November 12, 2011 at 8:32 am

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

Reply

John October 12, 2011 at 8:28 am

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

Reply

Sriram September 19, 2011 at 7:01 pm

Nice post dude.

Reply

boomboom August 16, 2011 at 2:44 am

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!

Reply

anu July 19, 2011 at 11:25 am

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

Reply

plas June 23, 2011 at 2:48 pm

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

Reply

cronosold June 7, 2011 at 12:05 am

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

Reply

vishal May 24, 2011 at 10:34 am

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

Reply

mc jam bi April 17, 2011 at 1:51 am

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

Reply

Leave a Comment

Previous post:

Next post: