How can I detect whether a browser window is focused or not?
<p>My page should make notification sounds only when the window is in the background.  I can track the <code>window.onfocus</code> and <code>window.onblur</code> events to notice when the focus state changes.  However, I don't know whether the window will load focused or not, since it might load in a background tab, for instance.</p>

<p>How do I decide whether to play sounds or not before I get an <code>onfocus</code>/<code>onblur</code> event?</p>
