Is it possible to automatically close a web browser window (e. g., in Google Chrome) when the user clicks outside the window? In order to do this, there would need to be some method for detecting whether or not the window was focused, and also a method for closing the window.
Asked
Active
Viewed 1,207 times
0
-
I suppose I could use window.close() to close the window, but I'd still need some way to detect whether or not the window was focused. – Anderson Green Nov 17 '12 at 20:49
-
Details about detecting window focus can be found here: http://stackoverflow.com/questions/3479734/test-if-window-has-focus – Anderson Green Nov 17 '12 at 20:50
-
1You can edit your question to add what you wrote as comments. – Dennis Traub Nov 17 '12 at 20:51
-
Just out of interest what is the use case for this? – Martin Smith Nov 17 '12 at 20:52
2 Answers
1
Most current browsers only let you close the child windows of a parent.
You can't close the master parent window through a script.

Michael Durrant
- 93,410
- 97
- 333
- 497
-
In that case, how can I use Javascript to close a child window? – Anderson Green Dec 14 '12 at 06:44
0
By default most browsers do not allow javascript to close windows that were not opened by javascript itself. But if you use e.g. window.open(...)
you can close that window with window.close()
.
So if you have a window opened by javascript you can do the following:
window.addEventListener('blur', function(){window.close();}, false);

AmShaegar
- 1,491
- 9
- 18