最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detecting tab visibility in Firefox - Stack Overflow

programmeradmin2浏览0评论

I'm writing a webapp that performs an action every minute or so which (very briefly) hangs the browser. I'd like to pause this action when the tab displaying the webapp is not shown, to minimize the annoyance. Is there any way to do this using Javascript, under the latest version of Firefox?

Edit: to clarify, I'm asking about how to determine the visibility of the tab that some JS code is running in - not how to pause/resume the action which hangs the browser.

I'm writing a webapp that performs an action every minute or so which (very briefly) hangs the browser. I'd like to pause this action when the tab displaying the webapp is not shown, to minimize the annoyance. Is there any way to do this using Javascript, under the latest version of Firefox?

Edit: to clarify, I'm asking about how to determine the visibility of the tab that some JS code is running in - not how to pause/resume the action which hangs the browser.

Share Improve this question edited Dec 3, 2009 at 16:51 Matt Ball asked Dec 3, 2009 at 16:39 Matt BallMatt Ball 360k102 gold badges653 silver badges720 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7
var isFocused = true;

window.onblur=function(){
  if(isFocused==true){
    isFocused=false;
  }
}

window.onfocus = function(){
  isFocused = true;
}

Now, that action you perform, every minute or two, do it only when isFocused is true. That is when the tab of your page/webapp is focused.

Not tested, but how about this:

window.onblur() = function () { ... pause your script here }

I don't know if Firefox handles the window blur in tabs as separate windows.

I believe you will need to make do with the window.onblur event, because accessing the browser window from a script in a webpage is forbidden for security reasons (only allowed for privileged scripts).

What you want to do (access the browser window from within a child window or a webpage) is described in Mozilla Developer Center, but it does mention there that only a privileged script can do it, and you'll probably get a "Premission denied" error when you try.

发布评论

评论列表(0)

  1. 暂无评论