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.



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

Method of dom/Documentdom/Document


var boolean = document.hasFocus();

Return Value

Returns an object of type BooleanBoolean

Returns true if the document has focus, otherwise returns false


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.

// Function displays the message DIV when the main document has focus
function fnOpenMessage()
    if (document.hasFocus())
    { = "block";
function fnCloseMessage()
{ = "none";

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

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


Related specifications

Candidate Recommendation