Skip to content

Instantly share code, notes, and snippets.

@terry-fei
Created November 20, 2015 11:41
Show Gist options
  • Save terry-fei/90c62c7fc034a22f52a6 to your computer and use it in GitHub Desktop.
Save terry-fei/90c62c7fc034a22f52a6 to your computer and use it in GitHub Desktop.
/*!
* domready (c) Dustin Diaz 2012 - License MIT
*/
var domready = (function domready() {
var fns = [],
listener,
doc = document,
hack = doc.documentElement.doScroll,
domContentLoaded = 'DOMContentLoaded',
loaded = (hack ? /^loaded|^c/ : /^loaded|^i|^c/).test(doc.readyState);
if (!loaded) {
doc.addEventListener(domContentLoaded, listener = function () {
doc.removeEventListener(domContentLoaded, listener);
loaded = 1;
while (listener = fns.shift()) { // jshint ignore:line
listener();
}
});
}
return function (fn) {
if (loaded) {
setTimeout(fn, 0);
} else {
fns.push(fn);
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment