Created
January 9, 2013 09:19
-
-
Save egm0121/4491804 to your computer and use it in GitHub Desktop.
Async script cross browser dom Ready detection
This file contains 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
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