-1

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?

3 Answers3

0

Use JavaScript for this. Call the below after whatever event that triggers the countdown:

window.setInterval(window.close,40000);
Greg Jennings
  • 1,611
  • 16
  • 25
0

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.

Community
  • 1
  • 1
0

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>
Jomar Sevillejo
  • 1,648
  • 2
  • 21
  • 33