user1534664
user1534664

Reputation: 3418

window tab pressed event

I want my html5 canvas game to automatically pause when the user opens up another tab inside the browser. What is the name of the event that is fired when a user does this?

Upvotes: 0

Views: 83

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 600120

As an alternative to the blur event suggested in another answer, you could use the new window.hidden property of HTML5.

Good places to read up on this are:

The biggest problem using this specification today is that you'll have to cater for vendor prefixes to handle it in all browsers. But if you ignore that it really comes down to:

if (!window.hidden) {
  // do whatever you normally do to render a frame
}

There are corresponding visibilitychange events in case you'd prefer to keep the detection out of your game loop.

Upvotes: 0

Talha Akbar
Talha Akbar

Reputation: 10040

$(window).blur( function() {

});

or in js:

window.onblur = function() {

}

There is no "new tab opened" event. But pausing on the blur event will do what you want - whenever the window loses focus the game will pause. You can then resume game in the focus event.

Upvotes: 2

Related Questions