How to detect that the window is unfocused?
Or more specifically - how (or actually - can you) detect if the current window has focus (i.e. it is the activ开发者_Python百科e window) when the window just opens?
I know I can listen for window.onblur
and window.onfocus
, but I'm trying to figure out how to address users that "open link in background tab/window" and the code starts running without either the onblur
or onfocus
events being called.
Unfortunately, You cannot detect if window has focus in Javascript. You can only notice when it get or lost focus using onfocus
and onblur
, as You said.
Some Flash video players start playing when the window receives focus. So, it seems that there is at least a way to do this in Flash (I'm no expert!). If there's no pure JavaScript way of achieving this (I can't think of any hacks at the moment), you could embed an invisible Flash applet that notifies your JavaScript code when the window receives focus.
精彩评论