browser page visibility function

Subject:

/**   API FOR PAGE VISIBILITY DETECTION*   IF USER TABS OUT TO ANOTHER TAB*   IF USER MINIMIZES THE BROWSER*/(function() {  var hidden = "hidden";

  // Standards:  if (hidden in document)    document.addEventListener("visibilitychange", onchange);  else if ((hidden = "mozHidden") in document)    document.addEventListener("mozvisibilitychange", onchange);  else if ((hidden = "webkitHidden") in document)    document.addEventListener("webkitvisibilitychange", onchange);  else if ((hidden = "msHidden") in document)    document.addEventListener("msvisibilitychange", onchange);  // IE 9 and lower:  else if ("onfocusin" in document)    document.onfocusin = document.onfocusout = onchange;  // All others:  else    window.onpageshow = window.onpagehide    = window.onfocus = window.onblur = onchange;

  function onchange (evt) {    var v = "visible", h = "hidden",        evtMap = {          focus:v, focusin:v, pageshow:v, blur:h, focusout:h, pagehide:h        };

    evt = evt || window.event;    if (evt.type in evtMap){//initial fire of page        document.body.className = evtMap[evt.type];        console.log(v);    }else{//after page logs if user leaves and comes back        document.body.className = this[hidden] ? "hidden" : "visible";        if(document.body.className == 'visible'){            //alert('checking authentication...');//works        }  }}

  // set the initial state (but only if browser supports the Page Visibility API)  if( document[hidden] !== undefined )    onchange({type: document[hidden] ? "blur" : "focus"});    alert(document[hidden]);})();

2016-07-20 09:14:00gstlouis

/*
*   API FOR PAGE VISIBILITY DETECTION
*   IF USER TABS OUT TO ANOTHER TAB
*   IF USER MINIMIZES THE BROWSER
*/
(function() {
  var hidden = "hidden";

  // Standards:
  if (hidden in document)
    document.addEventListener("visibilitychange", onchange);
  else if ((hidden = "mozHidden") in document)
    document.addEventListener("mozvisibilitychange", onchange);
  else if ((hidden = "webkitHidden") in document)
    document.addEventListener("webkitvisibilitychange", onchange);
  else if ((hidden = "msHidden") in document)
    document.addEventListener("msvisibilitychange", onchange);
  // IE 9 and lower:
  else if ("onfocusin" in document)
    document.onfocusin = document.onfocusout = onchange;
  // All others:
  else
    window.onpageshow = window.onpagehide
    = window.onfocus = window.onblur = onchange;

  function onchange (evt) {
    var v = "visible", h = "hidden",
        evtMap = {
          focus:v, focusin:v, pageshow:v, blur:h, focusout:h, pagehide:h
        };

    evt = evt || window.event;
    if (evt.type in evtMap){//initial fire of page
        document.body.className = evtMap[evt.type];
        console.log(v);
    }else{//after page logs if user leaves and comes back
        document.body.className = this[hidden] ? "hidden" : "visible";
        if(document.body.className == 'visible'){
            //alert('checking authentication...');//works
        }
  }
}

  // set the initial state (but only if browser supports the Page Visibility API)
  if( document[hidden] !== undefined )
    onchange({type: document[hidden] ? "blur" : "focus"});
    alert(document[hidden]);
})();

gstlouis
vote
2016-07-20 09:14:06