Does anyone know if there is a way to close a browser window if the user clicks anywhere outside the window?
-
2Possibly, but definitely not with PHP – Jan 14 '14 at 12:50
-
`if(user_clicked_elsewhere){window.close();}` But this 1. not possible with php and 2. its pretty annoying, you sure you want to have that? – Realitätsverlust Jan 14 '14 at 12:50
-
Sounds like you're looking to really annoy your users :/ If you could add some more information about how/where you are planning to use this, perhaps we'll be able to suggest some alternatives. For example, there is an event that is fired whenever an element looses focus. – Lix Jan 14 '14 at 12:51
-
Definitely not possible with PHP, as it runs server side, not client side. You could do a compromise with Javascript, where if nothing has had focus/been interacted with for a few minutes then the window closes, but afaik that's the closest you'll be able to get to your desired behaviour. – SubjectCurio Jan 14 '14 at 12:54
3 Answers
Not with PHP or JavaScript. The DOM is centered around the window. Anything that happens outside of it is outside the scope of the browser.
You might try wrapping your code in an application wrapper such as TideSDK, but even then, you probably won't be able to achieve this using web technologies...

- 52,573
- 26
- 113
- 168
(EDIT: Question has been re-tagged from PHP to Javascript since I wrote this answer)
No, PHP is a server-side language. That means it's usually finished running the script before the user sees the page in his/her browser.
A client-side script may be able to close a window, but usually only if it opened that window in the first place. See this question: Is it possible to close browser window from JavaScript?
One problem though is that the client-side script won't be able to detect clicks outside the browser window. The best you could really do is detect the cursor going outside the webpage for a period of time.

- 1
- 1

- 5,578
- 26
- 37
(RETAGED FROM PHP TO JAVASCRIPT WHILE I WAS WRITING THIS)
PHP is a server-side scripting language.. means that it can't do anything after the webpage has been loaded. So as what I got from your question, you have a div and if the user clicked out of it, it would close the browser, could be possible but not with php.. try jQuery.

- 549
- 1
- 11
- 23