HTML DOM hasFocus() Method Last Updated : 12 Jun, 2023 Comments Improve Suggest changes 2 Likes Like Report In HTML document, the document.hasfocus() the method is used for indicating whether an element or document has the focus or not. The function returns a true value if the element is focused otherwise false is returned. This method can be used to determine whether the active element is currently in focus. Syntax: document.hasfocus(); Parameters: This method has no parameters. Return Value: The hasfocus() method returns a Boolean value indicating whether an element or document has focus or not. The below examples illustrate the HTML DOM hasfocus() method: Example 1: This example illustrates whether the document has a focus or not. HTML <!DOCTYPE html> <html> <title> HTML DOM hasFocus() Method </title> <body> <p> Click anywhere in the document to test hasfocus() function. </p> <p id="para"></p> <script> setInterval("hasfocustest()", 1); function hasfocustest() { let x = document.getElementById("para"); if (document.hasFocus()) { x.innerHTML = "The document has focus."; } else { x.innerHTML = "The document DOES NOT have focus."; } } </script> </body> </html> Output: Explanation: The setinterval() function calls the hasfocustest() in 1 millisecond which after evaluation produces a result. Example 2: This example illustrates changes background-colour of the heading based on whether the document has focus or not. HTML <!DOCTYPE html> <html> <head> <title> HTML DOM hasFocus() Method </title> </head> <body> <p> Click anywhere in the document to test hasfocus() function. </p> <h1 id="para"> Function Testing</h1> <script> setInterval("hasfocustest()", 2000); function hasfocustest() { let x = document.getElementById("para"); if (document.hasFocus()) { x.style.background = "palegreen"; } else { x.style.background = "white"; } } </script> </body> </html> Output: Supported Browser: The browsers supported by the method are listed below: Google Chrome 1Edge 12Internet Explorer 5.5Firefox 3Opera 15Safari 4 Create Quiz Comment C chaitanyashah707 Follow 2 Improve C chaitanyashah707 Follow 2 Improve Article Tags : JavaScript Web technologies HTML-DOM HTML-Methods Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like