开发者

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

开发者 https://www.devze.com 2023-01-26 05:14 出处:网络
For example, if user is doing other things but not viewin开发者_如何学Pythong this page, jquery/javascript can know?

For example, if user is doing other things but not viewin开发者_如何学Pythong this page, jquery/javascript can know?

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


You can try this:

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

And:

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消