2

I've read:

Determine if window is active in webkit

and tried the demo at:

http://www.thefutureoftheweb.com/demo/2007-05-16-detect-browser-window-focus/

But that works on focus, and focus can be lost to another window, while the page is still visible (if the other window is not maximised).

What I'm looking for is a value which is true when the page is visible and false when it is not (or events on the transitions).

Edit: I suppose what I'm essentially looking for is "is this browser messing with my setIntervals and setTimeouts".

Edit: It looks like this is being sorted out: https://developer.mozilla.org/en/API/PageVisibility/Page_Visibility_API

Community
  • 1
  • 1
fadedbee
  • 42,671
  • 44
  • 178
  • 308
  • 1
    Not sure whether it'll do much more then the got/lost focus but saw [this anser](http://stackoverflow.com/a/7311159/1114171) which looked interesting. – T I Jan 09 '12 at 15:59
  • @Tom, onpagehide doesn't fire in Chrome latest :-( and onpageshow fires once only. – fadedbee Jan 09 '12 at 16:16
  • What exactly are you looking for? I don't understand what part of that demo isn't working for you. – gen_Eric Jan 09 '12 at 16:31
  • 2
    @Rocket: I think the demo is a red herring. The OP wants to detect when the page becomes visible or invisible (say, due to switching to another tab); the linked demo detects when the page gains or loses the focus (say, due to clicking in the location-bar). – ruakh Jan 09 '12 at 16:40
  • @ruakh - exactly right. If you have two browser windows open in two screens, both are visible, but only one has focus. – fadedbee Jan 10 '12 at 08:42

3 Answers3

1

You can't. User Agents don't give away this information.

After all, it doesn't make too much sense IMHO. What if the user has two broser windows and one of them is partially visible? fully hidden? both inactive? covered by some other application?

I think this is as close as you'll get: add mousemove, focus, keydown and scroll listeners to your body element, and expect the page to be visible for another x second timeout, where I have no idea what reasonable values for x may be.


Update: As a response to the comments below, here is a snippet that detects wether intervals are working as they should:

(function () {

    var requiredResolution = 10; // ms
    var checkInterval = 1000; // ms
    var tolerance = 20; // percent


    var counter = 0;
    var expected = checkInterval / requiredResolution;
    //console.log('expected:', expected);

    window.setInterval(function () {
        counter++;
    }, requiredResolution);

    window.setInterval(function () {
        var deviation = 100 * Math.abs(1 - counter / expected);
        // console.log('is:', counter, '(off by', deviation , '%)');
        if (deviation > tolerance) {
            console.warn('Timer resolution not sufficient!');
        }
        counter = 0;
    }, checkInterval);

})();
user123444555621
  • 148,182
  • 27
  • 114
  • 126
  • I suppose what I'm essentially looking for is "is this browser messing with my setIntervals and setTimeouts". – fadedbee Jan 10 '12 at 08:43
  • 1
    Ah, now I got your point. I wish there were an API for that indeed. Things like http://codecs.ofmlabs.org/ get seriously messed up when intervals are not working as expected. – user123444555621 Jan 10 '12 at 10:47
  • It looks there is not API for "is this browser messing with my setIntervals and setTimeouts" :-( – fadedbee Jan 19 '12 at 08:45
0

Try

if (/*@cc_on!@*/false) { // check for Internet Explorer
    document.onfocusin = onFocus;
    document.onfocusout = onBlur;
} else {
    window.onfocus = onFocus;
    window.onblur = onBlur;
}

and then set those functions.

Givingt credit where it's due... See:

Is there a way to detect if a browser window is not currently active?

Community
  • 1
  • 1
Rebecca
  • 577
  • 4
  • 11
0

Turns out the W3C is currently working on making a standard:

https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/PageVisibility/Overview.html#sec-page-visibility

However, at the moment, this is only supported by Chrome.

Demo

user123444555621
  • 148,182
  • 27
  • 114
  • 126