HTML DOM hasFocus() method
Example
Output text information after the document gains focus:
var
x = document.getElementById(
"demo"
);
if
(document.hasFocus()) {
x.innerHTML =
"The document has gained focus."
;
}
else
{
x.innerHTML =
"The document has lost focus."
;
}
Try it »
Definition and usage
The hasFocus() method returns a Boolean value used to detect whether the document (or any element within the document) has gained focus.
Browser support
The numbers in the table indicate the first browser version that supports this method.
| Method | |||||
|---|---|---|---|---|---|
| hasFocus() | 30.0 | 6.0 | 3.0 | Yes | 23.0 |
Syntax
document.hasFocus()
Parameters
| None |
|---|
Technical description
| Return value: | A Boolean value used to detect whether the document or an element has gained focus:
Element object