document.hasFocus to check window active or not

Returns boolean value.
var window_status=document.hasFocus();
We will get True if window is in focus ( or active ) or False if it is not in focus.

Checking the status of focus

By using if condition checking we can display message to user.
<script language=javascript>
if ( document.hasFocus() ) {
// window is in focus
}else{
// window is NOT in focus
}
</script>
We will use one timer to check the status of the window in every one second.
<script language=javascript>
//////////////////
function display_c(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('display_msg()',refresh)
}
////////////////////////////
function display_msg() {
if ( document.hasFocus() ) {
document.getElementById('my_msg').style.backgroundColor = "#00f000";	
document.getElementById('my_msg').innerHTML = "This document is in Focus ";
}else{
document.getElementById('my_msg').style.backgroundColor = "#f07814";
document.getElementById('my_msg').innerHTML = "This document is NOT in Focus ";	
}
display_c();
}
//////////////
</script>
How to Set Focus on window
DEMO of window.focus and document.hasFocus() method
document Object Last Modified date of the page
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com










    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer