Files
visibilityjs/lib/visibility.fallback.js
Andrey A.I. Sitnik 6fb36c4cc5 Clean up code
2012-07-06 00:59:06 +04:00

76 lines
2.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* 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 doesnt 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 59 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);
}
})();