use the hasFocus method of the document.
You can find detailed description and an example here:
hasFocus method
EDIT: Added fiddle
http://jsfiddle.net/Msjyv/3/
HTML
Currently <b id="status">without</b> focus...
JS
function check()
{
if(document.hasFocus() == lastFocusStatus) return;
lastFocusStatus = !lastFocusStatus;
statusEl.innerText = lastFocusStatus ? 'with' : 'without';
}
window.statusEl = document.getElementById('status');
window.lastFocusStatus = document.hasFocus();
check();
setInterval(check, 200);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…