public boolean hasFocus() {
   try {
     return ((Boolean)
             window.eval(
                 "var result=false;if(window.parent.document.hasFocus){result=window.parent.document.hasFocus();}else{result=true;}result;"))
         .booleanValue();
   } catch (Exception e) {
     // runs even after you close the window!
     return false;
   }
 }