Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The JavaScript FAQ
Prev Home Next

Closing a window

Question: How do I close a window?

Answer: To close a window that you previously opened from your script, you can use the window.close() method, for example:

winRef.close();
where winRef is the window reference, as returned by the window.open() method. The gotcha here is that the window referenced by winRef does not necessarily exist at the time when your script attempts to close it. (The user might well have closed it already!) And if the window no longer exists, your script may cause errors. Possible workarounds are the following:
  • Re-open the window with the same winRef and name.
  • Place the window-closing code only in the window to be closed. In this case the window reference will be self, and the code will be self.close().
  • Test whether the window is still open before attempting to close it.

If your script tries to execute window.close() for the main browser window that was opened by the user, most browsers will first invoke a dialog to ask the user whether to let the script close the browser window. Test this yourself: here is a script that attempts to close your browser window.

JavaScripter.net. Copyright © 1999-2006, Alexei Kourbatov

The JavaScript FAQ
Prev Home Next


 
 
  Mirrored with kind permission of Alexei Kourbatov Design by Interspire