title: Is there a reliable way to determine if a browser tab or window is inactive or not in focus?
body: <p>I've got a javascript timer that is making XMLHTTP requests on a constant basis (once every 10 seconds).  I'd love to be able to pause the timer when the window or tab loses focus.</p>
  
  <p>I'm fully aware of the <code>onFocus</code> and <code>onBlur</code> events on the <code>window</code> object, but they don't fire reliably across all browsers.  For instance, <a href="http://lists.macosforge.org/pipermail/webkit-unassigned/2009-February/101708.html" rel="noreferrer">in Safari, tabs don't trigger the events</a>.</p>
  
  <p>Simple code below distills the functionality I'm looking for:</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;Testing&lt;/title&gt;
      &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js"&gt;&lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;div id="console"&gt;&lt;/div&gt;
      &lt;script type="text/javascript"&gt;
        window.onfocus = function(event) {
          $('console').insert('Window gained focus&lt;br /&gt;');
        }
  
        window.onblur = function(event) {
          $('console').insert('Window lost focus&lt;br /&gt;');
        }
      &lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Does anyone out there have a technique for determining when a browser window or tab loses/gains focus that works across across all popular browsers?</p>