Skip to content

Instantly share code, notes, and snippets.

@djacobs
Forked from anonymous/simpledomready.js
Created February 9, 2011 23:48
Show Gist options
  • Save djacobs/819602 to your computer and use it in GitHub Desktop.
Save djacobs/819602 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