HTML DOM Document hasFocus() method

Definition and usage

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

Otherwise returns false.

Example

Show if the document has focus:

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

Try it yourself

Syntax

document.hasFocus()

Parameter

None.

Return value

Type Description
Boolean

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

Returns false otherwise.

Browser support

All browsers support document.hasFocus():

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support Support Support Support Support Support