views:

393

answers:

2

I have a flash object that takes up the whole browser on my website. I am trying to detect if the browser is in focus or not. What is the best way to do this? Using onfocus / onblur works in FireFox, but not in IE6 or IE7.

window.onblur = function() {
  document.title = "NOT focused";
}
window.onfocus = function() {
  document.title = "focused"
}

If I remove the flash object, it looks like this will work in IE6/7 also, but that is not an option for me. Thanks!

+1  A: 
if (/*@cc_on!@*/false) { // check for Internet Explorer
    document.onfocusin = function(){document.title = "focused";}
    document.onfocusout = function(){document.title = "NOT focused";}
} else {
    window.onfocus = function(){document.title = "focused";}
    window.onblur = function(){document.title = "NOT focused";}
}
James Skidmore
This still had a few misfires when going in and out of tabs, but was definitely better than what I had!
Andrew
+1  A: 

In AS3 you can add an event listener to the stage:

stage.addEventListener(Event.DEACTIVATE, windowNotActiveCallback);
stage.addEventListener(Event.ACTIVATE, windowActiveCallback);
tom
This worked great - thanks!
Andrew