HTML DOM Document hasFocus() method

Definition and usage

If the document (or any element within the document) has gained focus hasFocus() The method returns true.

Otherwise return false.

Example

Show if the document has focus:

if (document.hasFocus()) {
  text = "The document has focus.";
} else {
  text = "The document does NOT have focus.";
}

Try it yourself

Syntax

document.hasFocus()

Parameter

None.

Return value

Type Description
Boolean value

Returns true if the document (or any element within the document) has focus.

Otherwise return false.

Browser support

All web browsers support document.hasFocus()

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Stöd Stöd Stöd Stöd Stöd Stöd