Skip to content

Instantly share code, notes, and snippets.

Created January 13, 2011 21:49
Show Gist options
  • Save anonymous/778685 to your computer and use it in GitHub Desktop.
Save anonymous/778685 to your computer and use it in GitHub Desktop.
function domReady(callback) {
var domLoaded;
var called;
var onReadyStateChange = function () {
if (document.readyState == 'complete') {
domLoaded();
}
};
var interval;
var pollForReady = function () {
try {
document.documentElement.doScroll('left');
domLoaded();
}
catch(e) {}
};
if (window.addEventListener) {
domLoaded = function() {
if (!called) {
called = true;
callback();
}
window.removeEventListener('DOMContentLoaded', domLoaded, false);
window.removeEventListener('load', domLoaded, false);
};
window.addEventListener('DOMContentLoaded', domLoaded, false);
window.addEventListener('load', domLoaded, false);
} else if (window.attachEvent) {
interval = window.setInterval(pollForReady, 13);
domLoaded = function() {
if (!called) {
called = true;
callback();
}
window.clearInterval(interval);
window.detachEvent('onreadystatechange', onReadyStateChange);
window.detachEvent('onload', domLoaded);
};
window.attachEvent('onreadystatechange', onReadyStateChange);
window.attachEvent('onload', domLoaded);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment