Document hasFocus() Method - Javascript DOM

Javascript examples for DOM:Document hasFocus

Description

The hasFocus() method returns the document has focus.

Parameters

None

Return Value:

A Boolean value, whether the document has focus

The following code shows how to Output some text if the document has focus:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
setInterval("myFunction()", 1);

function myFunction() {//w  ww. j  ava2 s.  co  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>

Related Tutorials