5

Specific to google chrome: I'm trying to detect when a page loses focus, but not when it loses focus to the javascript console / other dev tools (i.e. the page is still the primary focus).

Any ideas?

I've tried using window.onblur and visibilitychange events.

Short use case: I'm working on a game and for some reason when it's running my editor (sublime text 2 on OSX) slows to a crawl. If I change window focus it's fine. So I'm trying to pause the game when I go to my editor but leave it running when I'm in the javascript console

[edit]

Since my question is apparently unclear, trying to clarify: I want to be able to tell the difference between:

  • switching tabs, switching apps
  • going to the javascript console
  • 1
    @Givi - `document.hasFocus()` is always true in chrome, so no go :( There is a [bug](https://code.google.com/p/chromium/issues/detail?id=64846) on this from late 2010 that hasn't been fixed yet –  Jun 05 '13 at 01:00
  • 1
    http://stackoverflow.com/questions/1060008/is-there-a-way-to-detect-if-a-browser-window-is-not-currently-active has a great snippet – exussum Jun 05 '13 at 01:03
  • hmm it's works for me try this and then switch window setTimeout(function() { alert(document.hasFocus()); }, 2000); – Givi Jun 05 '13 at 01:04
  • 1
    @Givi - I did, I put it on a timer every second, I get nothing but "true", even when switching tabs / apps –  Jun 05 '13 at 01:07
  • It works for me like clockwork ;) – Givi Jun 05 '13 at 01:12
  • document.body.onblur will work for non focused elements. See http://jsfiddle.net/kqEZn/ – WhyMe Jun 05 '13 at 01:12
  • @Givi - chrome version / OS? –  Jun 05 '13 at 01:13
  • Version 27.0.1453.94 m / Windows 7 Ultimate – Givi Jun 05 '13 at 01:16
  • @Givi - maybe it's an OSX issue then, none of my versions of chrome say anything other than "true" :) –  Jun 05 '13 at 01:21

2 Answers2

0

Because you can't really detect if the focus is on the dev tool or not, the best you can do is to detect if you opened the dev tool:

var focused = true;

$(window).blur(function () {
    setTimeout(function () {
        if(!isInspectOpen()) {
            alert("Focus lost.");
            focused = false;
        }
    }, 500);
});
$(window).focus(function () {
    if(!focused) {
        alert("Focus gained.");
        focused = true;
    }
});

function isInspectOpen() {
    console.profile();
    console.profileEnd();
    if(console.clear) console.clear();
    return console.profiles.length > 0;
}

               http://jsfiddle.net/DerekL/NtPPR/

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • Interesting idea :) Any ideas for how to do this in chrome? `console.profiles` doesn't exist –  Jun 05 '13 at 01:20
  • haha, stopped working less than two weeks ago: https://codereview.chromium.org/15816002 –  Jun 05 '13 at 01:27
  • maybe, I'm using dev/canary which are both at 29, and even if the other channels are still at 28, this is going to be gone soon :( –  Jun 06 '13 at 02:56
  • How would you write this with vanilla javascript and not jQuery? – Leon Gaban Aug 10 '15 at 21:34
-1

window.onfocus? I really don't understand what do you mean but look at this references. Search for "events":

DOM document reference

DOM reference

Hope it help you!

Community
  • 1
  • 1
gal007
  • 6,911
  • 8
  • 47
  • 70