Output some text if the document has focus:
<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> setInterval("myFunction()", 1); function myFunction() {//w w w . jav a 2s . c o m var x = document.getElementById("demo"); if (document.hasFocus()) { x.innerHTML = "The document has focus."; } else { x.innerHTML = "The document DOES NOT have focus."; } } </script> </body> </html>
The hasFocus()
method returns a Boolean value indicating whether the document or any element inside the document has focus.
document.hasFocus()