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="javascript: self.close ()">Close this Window</A>

Note:
You can create full featured, great looking popups without any programming.
Click here for more info

Sample Code for window.close()

<html>
<head>
 <title>JavaScript Window Close Example </title>
</head>
<SCRIPT language="JavaScript1.2">
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.

<<Prev: Using the window.open method JavaScript Popup Windows

  • Digg
  • del.icio.us
  • Netscape
  • Reddit
  • StumbleUpon
  • Technorati
  • YahooMyWeb

How to make a form without coding? Click here for a demo.

Follow me on twitter

HTML Forms
HTML Form Tutorial
The Form Tag
The Form Submit Button
Form design software
How to make a web form
Make web forms quickly, without coding

Javascript Form Handling
  Basics
Using getElementById to get the elements in a form
Set value of form element using JavaScript
Get value of form element using JavaScript
Handling multiple forms using JavaScript
JavaScript reset/clear a form
  Advanced
JavaScript Form Validation
Submitting a form using JavaScript
Can JavaScript Email a Form?
Switching the form action field dynamically
JavaScript Button

JavaScript Popup Windows
The window.open method
The window.close method
More ...

 
.   Copyright © 2003-2009 JavaScript-coder.com. All rights reserved.