Skip to content

Instantly share code, notes, and snippets.

@egm0121
Created January 9, 2013 09:19
Show Gist options
  • Save egm0121/4491804 to your computer and use it in GitHub Desktop.
Save egm0121/4491804 to your computer and use it in GitHub Desktop.
Async script cross browser dom Ready detection
var _ready = function (doc) {
function dispatch() {
hasRun = 1;
for (var i = 0, l = queue.length; i < l; i++) queue[i]()
}
var hasRun = 0,
queue = [],
d, e, f = !1,
aElem = doc.createElement("a"),
dloaded = "DOMContentLoaded",
bind = "addEventListener",
ready = "onreadystatechange";
/^loade|c/.test(doc.readyState) && (hasRun = 1), doc[bind] && doc[bind](dloaded, me = function () {
doc.removeEventListener(dloaded, me, f), dispatch()
}, f), aElem.doScroll && doc.attachEvent(ready, d = function () {
/^c/.test(doc.readyState) && (doc.detachEvent(ready, d) , dispatch());
});
var check = aElem.doScroll ? function (callback) {
self != top ? hasRun ? callback() : c.push(callback) : ! function () {
try {
aElem.doScroll("left")
} catch (b) {
return setTimeout(function () {
check(callback)
}, 50)
}
callback()
}()
} : function (callback) {
hasRun ? callback() : queue.push(callback)
};
return check;
}(document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment