The hasFocus() method of the Document interface returns a Boolean value indicating whether the document or any element inside the document has focus. This method can be used to determine whether the active element in a document has focus.

When viewing a document, an element with focus is always the active element in the document, but an active element does not necessarily have focus. For example, an active element within a popup window that is not the foreground doesn't have focus.


var focused = document.hasFocus();

Return value

false if the active element in the document has no focus; true if the active element in the document has focus.


This example checks whether the document has focus every 300 milliseconds. To test the functionality of hasFocus(), click on the button to open a new window, and try switching between the two pages.


<p id="log">Awaiting focus check.</p>
<button onclick="openWindow()">Open a new window</button>


function checkPageFocus() {
  let body = document.querySelector('body');
  let log = document.getElementById('log');

  if (document.hasFocus()) {
    log.textContent = 'This document has the focus.'; = '#fff';
  else {
    log.textContent = 'This document does not have the focus.'; = '#ccc';

function openWindow() {'', 'MDN', 'width=640,height=320,left=150,top=150');

// Check page focus every 300 milliseconds
setInterval(checkPageFocus, 300);



Specification Status Comment
HTML Living Standard
The definition of 'Document.hasFocus()' in that specification.
Living Standard Initial definition

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
hasFocusChrome Full support 1Edge Full support 12Firefox Full support 3IE Full support 6Opera Full support 15Safari Full support 4WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 14Safari iOS Full support 3.2Samsung Internet Android Full support 1.0


Full support  
Full support

See also