Returns true if the webpage is in focus state; otherwise, returns fasle.
What is the focus? The focus is whether the user is active on this page. whether the mouse is active on the webpage. or whether the content on the webpage has been selected, or the cursor exists in an element on the page. if one of the conditions is met, the page is in the focus state. note that the hasFocus method is applicable only to webpages and not browsers. in the following instance. when you click a webpage. the webpage is in focus state. click the address bar of your browser. the webpage loses its focus and displays false. again, the hasFocus method can only run in the document Object. see the following example.
Copy codeThe Code is as follows:
<Html>
<Head>
<Title> Dom: hasFocus Method Instance </title>
</Head>
<Body onfocus = "getFocus ()" onblur = "Empty_Focus ()">
<H2> click the webpage area. This indicates that the webpage has a focus and is displayed as true. Click the address bar of the browser. If the webpage loses its focus, the page is displayed as false. <Hr/>
<A href = "http://www.jb51.net" onfocus = "getFocus ()"> Home of foot </a>
<Span id = "c"> </span>
<Script language = "javascript">
Function getFocus (){
Document. getElementById ("c"). innerHTML = document. hasFocus ();
}
Function Empty_Focus (){
Document. getElementById ("c"). innerHTML = document. hasFocus ();
}
</Script>
</Body>
</Html>