Corey
Corey

Reputation: 2563

Javascript countdown to window close

$(function(){
      var count = 5;
      countdown = setInterval(function(){
        $(".countdown").html("Closing in " + count + "...");
        if (count == 0) {
          window.close();
        }
        count--;
      }, 1000);
    });

Doesn't seem to be working for me. But if I change window.close(); to window.location = "http://google.com";, it works. Any help on closing a window after a countdown would be a great help!

Thank you!

Upvotes: 0

Views: 1695

Answers (3)

epascarello
epascarello

Reputation: 207501

You can not close windows that you did not make with window.open().

Would you want to go to some random site and all of the sudden your browser just closes? Most people do not want that so browser developers prevent it from happening. There are hacks that work, but most of the time browsers will fix those hacks and they stop working.

If you want to control it in a corporate environment, you can always have the user's run their browsers in kiosk mode.

Upvotes: 0

Corey
Corey

Reputation: 2563

Hmm, I was testing this in a tab in Chrome. Didn't work. But when I pop up my own window (which my app does), and then fire this code in that window, it works.

Upvotes: 0

Riju Mahna
Riju Mahna

Reputation: 6926

In which browser are you trying this ? Usually it is not that simple when working in browsers that have multiple tabs opened .. I used the below for opening and closing a window (single tab) and it worked for me

window.open('', '_self', ''); 
window.close();

Upvotes: 2

Related Questions