title: how to close a popup window when the focus is gone out from it
body: <p><strong>Requirement:</strong> A popup will be opened up from parent window, and it should get closed when the focus from the window is lost. (This should happen even when another application window is opened or came into focus).</p>
  
  <p><strong>Tried code:</strong> 
  <code>&lt;body onBlur='javascript:window.close();'&gt;</code></p>
  
  <p><strong>Issue:</strong>
  On click within the body of the popup makes the popup closed.</p>
  
  <p><strong>Compatibility:</strong> ie6 and above, firefox.</p>
  
  <p>I got a <strong>workaround</strong> from <strong><em><a href="http://pro-thoughts.blogspot.com/2006/10/incorrect-behavior-of-windowonblur.html" rel="nofollow">http://pro-thoughts.blogspot.com/2006/10/incorrect-behavior-of-windowonblur.html</a></em></strong></p>
  
  <pre><code>var active_element;  
  var bIsMSIE;
  
  function initiateSelfClosing() {  
    if (navigator.appName == "Microsoft Internet Explorer") {  
      active_element = document.activeElement;  
      document.onfocusout = closeWnd;  
      bIsMSIE = true;  
    }  
    else { window.onblur = closeWnd; }  
  }
  
  function closeWnd() {  
    if (window.opener != null) {  
      if (bIsMSIE &amp;&amp; (active_element != document.activeElement)) {  
        active_element = document.activeElement;  
      }  
      else {  
        window.close();  
      }  
    }  
  }  
  
  
  &lt;body onload="initiateSelfClosing()"&gt;  
  &lt;/body&gt;
  </code></pre>
  
  <p>But here also one problem is there, if there is a print button in the page, and if am clicking on print > then cancelling the print job, the popup is getting closed.</p>
  
  <p>Can some one help me pls...</p>