Bin Chen
Bin Chen

Reputation: 63299

jquery: How to know if a page is not in focus?

For example, if user is doing other things but not viewing this page, jquery/javascript can know?

I don't need to be very accurate, just roughly.

Upvotes: 2

Views: 444

Answers (2)

jAndy
jAndy

Reputation: 235992

You might want to use a blur or focusout event on your document or window.

$(document).bind('focusout', function() {
    // window or tab just lost the focus
});

that's using jQuery. You should play around with this, also use the window object as target. I'm not super sure how cross-browser this works, but it should perform well.

Upvotes: 4

Sarfraz
Sarfraz

Reputation: 382696

You can try this:

$(window).focus(function(){
  // back in focus
});

And:

$(window).blur(function(){
  // no focus 
});

Upvotes: 7

Related Questions