This page is Almost Ready

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

hasFocus

Summary

Returns the focus state of the current document, true if the document has focus, false if not.

Method of dom/Documentdom/Document

Syntax

var boolean = document.hasFocus();

Return Value

Returns an object of type BooleanBoolean

Returns true if the document has focus, otherwise returns false

Examples

The following example shows how to use the hasFocus method to determine if the document has focus. If you mouse over the text when the document has focus, “A message for you!” appears; if you mouse over the text when the document does not have focus, the message does not appear.

<!DOCTYPE HTML>
<html>
<head>
<script>
// Function displays the message DIV when the main document has focus
function fnOpenMessage()
{
    if (document.hasFocus())
    {
        oMessageDiv.style.display = "block";
    }
}
function fnCloseMessage()
{
    oMessageDiv.style.display = "none";
}
</script>
</head>
<body>

<p>
<span style= "color:darkmagenta;font-size:large;" onmouseout="fnCloseMessage();"
onmouseover="fnOpenMessage();">Mouse over this!</span>
</p>

<div id="oMessageDiv" style="display:none; font-family:arial; width:200px; font-size:large; color:steelblue; border:4px solid gold;">
A message for you!
</div>

</body>
</html>

Related specifications

W3C HTML5
Candidate Recommendation

Attributions