The JS script presented in this page can be used to execute a JavaScript function when the page visibility state changes (when it is opened another window, or focus again this page); the function is whenVisChg(), see the comments in code.

Script code - click to select it

//JS code from:
// Defines a handler for page visibility changes
var vis_chg = (function(){
  var stateKey, eventKey, keys = {
    hidden: 'visibilitychange',
    webkitHidden: 'webkitvisibilitychange',
    mozHidden: 'mozvisibilitychange',
    msHidden: 'msvisibilitychange'
  for(stateKey in keys){
    if(stateKey in document){
      eventKey = keys[stateKey];
  return function(c){
      document.addEventListener(eventKey, c);
      //document.addEventListener('focus', c); //for when focus again this page
    return !document[stateKey];

// Gives the initial state
var page_vstate = vis_chg() ?'visible' :'not-visible';

  var re_vchg = vis_chg(); //FALSE=leaves the page; TRUE=re-enter in page
  console.log(new Date, 'vis_chg: ', re_vchg); //for debug in console
  page_vstate = re_vchg ?'visible' : whenVisChg();

//This function is executed when the page changes visibility state
function whenVisChg(){
  document.title ='not-visible';  //changes page title in browser

  //HERE you can set your code
  alert('page visibility state changed');

  return 'not-visible';
- To test the script, click on , or just open a new tab /window in your browser. The Title of this page in browser will be changed and an alert message box will be displayed on this page.

Daily Test with Code Example

Which attribute is used in <a> tag for the address of the link?
src href rel
<a href="" title=""></a>
Which CSS property sets the type of the text font?
font-family text-decoration font-size
h2 {
What instruction selects all the <div> tags with class="cls"?
querySelector("div.cls") getElementsByTagName("div") querySelectorAll("div.cls")
var elm_list = document.querySelectorAll("div.cls");
var nr_elms = elm_list.length;       // number of selected items
Indicate the function that can be used to get the sum of values in an array.
array_sum() array_diff() array_shift()
$arr = array(1, 2, 3, 4);
$arr_sum = array_sum($arr);
echo $arr_sum;       // 10
Detect when page visibility state is changed

Last accessed pages

  1. Read Excel file data in PHP - PhpExcelReader (41451)
  2. Snowboard Challenge (141)
  3. Sport Games (3989)
  4. Escape Games (5818)
  5. Bubbles3 (25700)

Popular pages this month

  1. Qwop (4763)
  2. Bubbles3 (4403)
  3. Butterfly Kyodai (3133)
  4. Zuma Deluxe (2903)
  5. Backgammon (1486)