Javascript DOM HTML Document hasFocus() Method

Introduction

Output some text if the document has focus:

View in separate window

<!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()



PreviousNext

Related