Cross browser page active or not detection?

i’m interested in knowing when the page is active and when it’s not (another browser window, or tab, or different app, is in front). i found this code:

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

in both my browsers this works only partly and in different ways.

in an old mac safari blur happens when the app is changed away from the browser (and focus when changed back), and when a new browser window is made active (and focus when original window made active again), but doesn’t work with tabs; don’t get blur when a new tab is created and accessed or an already existing one is accessed (nor focus when switching tabs back to original tab).

in a mac firefox it works with tabs and different windows, but on app switch no blur event but there is a focus event when making browser app (with original window in front) active again.

presumably there’s going to be all manor of anomilies with other browsers. any one know of some already existing code which attempts to cope with these differences and get round some of them?

or, a couple of more specific questions: how to get safari to report tab changes, and how to get firefox to report the app losing focus?


no thoughts on this?

what about firefox? (version i’m talking about happens to be v2 on the mac but the problem may be on more recent ones i’m not sure). how to get it to acknowledge losing focus when the browser is no longer the app in the foreground? (it acknowledges focus when browser put back into the foreground.) or is there just no way to do that?


onfocus and onblur are not standard events for windows and so only some browsers support them - with more supporting onfocus than support onblur.

When a tab or window does lose the focus then you are not going to have any JavaScript run in that tab or window until it gets the focus back and so testing for it losing the focus from within JavaScript isn’t all that useful even where it is supported - you’d need to do the test from the window that actually gets the focus.

oh right, ok, thanks.