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>