开发者

Detecting tab visibility in Firefox

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 th开发者_StackOverflowe 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.


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜