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 = 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!

Comments on this entry are closed.

  • Nicolò ‘Nik’ Zanetti

    if(false == my_window.closed)
    Before writing a post, what about learning the basics of programming?
    You gave me cancer.

  • Vipul Malhotra

    I have a script in which after performing a task I am closing the current window.
    Issue is that the window i getting closed before my code is executed.
    Please let me know if anyone have any idea.
    I am using Greasemonkey to run my script on mozilla.

  • Sreeyush Sudhakaran

    why does’t work for

    Button1.Attributes.Add(“onclick”, “javascript: popuponclick()”)
    Button2.Attributes.Add(“onclick”, “javascript: closepopup()”)

  • Thanks for your great job.

    digital marketing course in chennai

  • Sanchit

    Thanks

  • Though I’m not a java expert it war very useful to me, in creating my own website.

    Singapore education consultants

  • gokul vijay

    They are offer thesoftware testing for hadoop programming language.This is very useful for you hadoop programming dots.I have read you article very useful information software testing. Thank you for sharing you article.Hadoop Courses in Chennai

  • Your information about Selenium scripts is really interesting.
    Also I want to know the latest new techniques which are implemented in
    selenium. Can you please update it in your website Best
    Selenium training institute in Chennai

  • jackson

    Hi, I wish to be a regular contributor of your blog. I have read your blog. Your information is really useful for beginner. Recently I have completed Software Testing Course in Chennai at a reputed institutes.Its really useful for me to make a bright carrer. If anyone want to get Manual Testing Training in Chennai reach FITA.