So your cool new app is perfect, but you want it to lock the user out when the browser hasn’t had focus after 15 minutes? Well that’s easy you think, I’ll just connect to the document’s blur and focus events and be good to go. You quickly add a little bit of code to your Dojo widget:

     dojo.connect(dojo.doc, "onblur", this, "onWindowBlur");
     dojo.connect(dojo.doc, "onfocus", this, "onWindowFocus");

That should do it you’d think. Launch your app with Firefox and everything is great, easy enough. The same is true with Safari. After reluctantly firing up your Virtual Machine to test Internet Explorer 6, much to your dismay, onfocus events are immediately followed by onfocusout events. You feel the harsh reality that IE6 is going to suck away a bit more of your life.

My new patch of gray hair is further evidence that I feel your pain.

Fortunately, while the solution seems to be difficult to track down on the “interwebs”, it is in fact pretty simple. The key is to understand that IE tracks the activeElement (document.activeElement) and fires onfocus and onfocusout events as the activeElement changes. As the onfocusout events occur, you must track what the activeElement is. When an onfocusout event happens and the activeElement doesn’t change, then the window has lost focus.

Take a look at the solution for IE:

        dojo.connect(dojo.global, "onfocus", this, "onWindowFocus");
        dojo.connect(dojo.doc, "onfocusout", this, function() {
             if (this._activeElement != document.activeElement){
                    this._activeElement = document.activeElement;
             }else{
                    this.onWindowBlur()
             }
        });

That’s not too bad at all. Here is is a cross browser compatible version:

if (dojo.isIE) {
   dojo.connect(dojo.global, "onfocus", this, "onWindowFocus");
        dojo.connect(dojo.doc, "onfocusout", this, function() {
             if (this._activeElement != document.activeElement){
                    this._activeElement = document.activeElement;
             }else{
                    this.onWindowBlur()
             }
        });
 }else{
    dojo.connect(dojo.doc, "onblur", this, "onWindowBlur");
    dojo.connect(dojo.doc, "onfocus", this, "onWindowFocus");
 }

There you go. Now you have time to play with your kids, get in a round of golf, and save a little bit of your life force for your next battle with Internet Explorer 6.