Well, I guess it's simple. I need an JavaScript that close my page automaticaty. Example: An user came to my site, then he may stay how long he wants, but after he click something on my site (for example he click the play button of an embed youtube video, or it doesn't matter what elese) then, from that moment, after 40 seconds the page will aoutomaticaly close. Is it possible?
-
maybe you're right it's simple, but have you tried something? – Vainglory07 Feb 19 '14 at 01:47
-
Not with PHP. PHP is a server-side technology and has not idea what is going on in the browser once the page has been rendered. – Mike Brant Feb 19 '14 at 01:47
-
But if I add a button, and once's clicked, then the countdown will start and after x seconds, the page will close. Al least this is possible? :D – user3314333 Feb 19 '14 at 01:52
-
You can only call `window.close()` if your window was created by javascript. – zer02 Feb 19 '14 at 01:55
3 Answers
Use JavaScript for this. Call the below after whatever event that triggers the countdown:
window.setInterval(window.close,40000);

- 1,611
- 16
- 25
Did you try to search your question?
How to close current tab in a browser window?
This method works in Chrome and IE:
<a href="blablabla" onclick="setTimeout(function(){var ww = window.open(window.location, '_self'); ww.close(); }, 1000);">
If you click on this the window will be closed after 1000ms
</a>
There are ways to do this, but they are "hack"ish.
JavaScript can not, for security reasons, close a window that it did not open. You can, however, use a trick to make the browser think your current window was opened by javaScript
and
You can't close any tab via JavaScript. "This method is only allowed to be called for windows that were opened by a script using the window.open method." In other words, you can only use JavaScript to close a window/tab that was spawned via JavaScript.

- 1
- 1
What you are planning is not possible with PHP alone, but you may let PHP generate javascript to do what you need.
It is not possible to close a page opened by the user. This is a rule browsers have implemented to prevent sending prank emails with such script. (Before it was implemented, there were emails that contained scripts which closes the current tab when opened.)
But I had the same problem as you before. To fix that problem, you need javascript to reopen the tab and quickly close it. This is the script I use for close buttons on my webapps:
function Close_Button(){
msg = confirm("Are you sure you want to Exit IES?");
if(msg){
setTimeout(function(){
window.open('', '_self', '');
window.close();
}, 10);
}
}
You can bind the same script to the click you were talking about and take away the confirm message. If you want it to close in 40 seconds, see the example bellow.
EDIT: The script that closes without confirm on timer:
function Close(timer){//timer = number of seconds
setTimeout(function(){
window.open('', '_self', '');
window.close();
}, timer * 1000);
}
Then you can trigger like this:
Close(5);
And bind it to an element's click event.
<div id="YouTubeVideoContainer" onclick="Close(40);">
Closes in 40 seconds.
</div>

- 1,648
- 2
- 21
- 33