-
-
Save edwardbc/4559614 to your computer and use it in GitHub Desktop.
- Updated with 'moz' prefix and a proper prefix verification method.
- Added a callback cleaning method.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* isVis - v0.5.6 Jan 2013 - Page Visibility API Polyfill | |
* Copyright (c) 2013 Addy Osmani | |
* Dual licensed under the MIT and GPL licenses. | |
*/ | |
(function () { | |
window.visibly = { | |
b: null, | |
q: document, | |
p: undefined, | |
prefixes: ['webkit', 'ms', 'moz'], | |
props: ['VisibilityState', 'visibilitychange', 'Hidden'], | |
m: ['focus', 'blur'], | |
visibleCallbacks: [], | |
hiddenCallbacks: [], | |
_callbacks: [], | |
onVisible: function ( _callback ) { | |
this.visibleCallbacks.push(_callback); | |
}, | |
onHidden: function ( _callback ) { | |
this.hiddenCallbacks.push(_callback); | |
}, | |
isSupported: function () { | |
var s = false; | |
for (var i = 0; i < this.prefixes.length; i++ ) { | |
if (this.prefixes[i]+this.props[2] in this.q){ | |
this.b = this.prefixes[i]; | |
s = true; | |
break; | |
} | |
} | |
return s; | |
}, | |
runCallbacks: function ( index ) { | |
if ( index ) { | |
this._callbacks = (index == 1) ? this.visibleCallbacks : this.hiddenCallbacks; | |
for (var i = 0; i < this._callbacks.length; i++) { | |
this._callbacks[i](); | |
} | |
} | |
}, | |
_visible: function () { | |
window.visibly.runCallbacks(1); | |
}, | |
_hidden: function () { | |
window.visibly.runCallbacks(2); | |
}, | |
_nativeSwitch: function () { | |
((this.q[this.b + this.props[2]]) === true) ? this._hidden() : this._visible(); | |
}, | |
listen: function () { | |
try { /*if no native page visibility support found..*/ | |
if (!(this.isSupported())) { | |
if (document.addEventListener) { /*for browsers without focusin/out support eg. firefox, opera use focus/blur*/ | |
/*window used instead of doc as Opera complains otherwise*/ | |
window.addEventListener(this.m[0], this._visible, 1); | |
window.addEventListener(this.m[1], this._hidden, 1); | |
} else { /*IE <10s most reliable focus events are onfocusin/onfocusout*/ | |
this.q.attachEvent('onfocusin', this._visible); | |
this.q.attachEvent('onfocusout', this._hidden); | |
} | |
} else { /*switch support based on prefix*/ | |
this.q.addEventListener(this.b + this.props[1], function () { | |
window.visibly._nativeSwitch.apply(window.visibly, arguments); | |
}, 1); | |
} | |
} catch (e) {} | |
}, | |
clean: function(){ | |
this.visibleCallbacks= []; | |
this.hiddenCallbacks= []; | |
this._callbacks= []; | |
}, | |
init: function () { | |
this.listen(); | |
} | |
} | |
this.visibly.init(); | |
})(); | |
/*usage**/ | |
visibly.onVisible(function () { | |
document.title = 'visible' | |
}); | |
visibly.onVisible(function () { | |
console.log('visible'); | |
}); | |
visibly.onHidden(function () { | |
document.title = 'hidden'; | |
}); | |
visibly.onHidden(function () { | |
console.log('hidden'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment