mirror of
https://github.com/ai/visibilityjs.git
synced 2026-01-14 07:47:57 -05:00
76 lines
2.6 KiB
JavaScript
76 lines
2.6 KiB
JavaScript
/*
|
||
* Copyright 2011 Andrey “A.I.” Sitnik <andrey@sitnik.ru>,
|
||
* sponsored by Evil Martians.
|
||
*
|
||
* This program is free software: you can redistribute it and/or modify
|
||
* it under the terms of the GNU Lesser General Public License as published by
|
||
* the Free Software Foundation, either version 3 of the License, or
|
||
* (at your option) any later version.
|
||
*
|
||
* This program is distributed in the hope that it will be useful,
|
||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
* GNU Lesser General Public License for more details.
|
||
*
|
||
* You should have received a copy of the GNU Lesser General Public License
|
||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
*/
|
||
|
||
// Add Page Visibility API support to old browsers by focus/blur hack.
|
||
//
|
||
// Include this script _before_ Visibility.js.
|
||
//
|
||
// Note, that this hack doesn’t correctly emulate Page Visibility API:
|
||
// when user change focus from browser to another window (browser and your
|
||
// page may stay visible), this hack will decide, that you page is hidden.
|
||
//
|
||
// For Firefox 5–9 it will be better to use MozVisibility hack without
|
||
// this issue. See <https://github.com/private-face/mozvisibility>.
|
||
;(function () {
|
||
"use strict";
|
||
|
||
if ( document.visibilityState || document.webkitVisibilityState ||
|
||
document.msVisibilityState || document.mozVisibilityState ||
|
||
document.oVisibilityState ) {
|
||
return;
|
||
}
|
||
|
||
document.hidden = false;
|
||
document.visibilityState = 'visible';
|
||
|
||
var event = null
|
||
var i = 0
|
||
var fireEvent = function () {
|
||
if( document.createEvent ) {
|
||
if ( !event ) {
|
||
event = document.createEvent('HTMLEvents');
|
||
event.initEvent('visibilitychange', true, true);
|
||
}
|
||
document.dispatchEvent(event);
|
||
} else {
|
||
if ( typeof(Visibility) == 'object' ) {
|
||
Visibility._onChange.call(Visibility, { })
|
||
}
|
||
}
|
||
}
|
||
|
||
var onfocus = function () {
|
||
document.hidden = false;
|
||
document.visibilityState = 'visible';
|
||
fireEvent();
|
||
};
|
||
var onblur = function () {
|
||
document.hidden = true;
|
||
document.visibilityState = 'hidden';
|
||
fireEvent();
|
||
}
|
||
|
||
if ( document.addEventListener ) {
|
||
window.addEventListener('focus', onfocus, true);
|
||
window.addEventListener('blur', onblur, true);
|
||
} else {
|
||
document.attachEvent('onfocusin', onfocus);
|
||
document.attachEvent('onfocusout', onblur);
|
||
}
|
||
})();
|