Писал код для себя, похоже это то что вам нужно
(function(handler) {
var rl = [];
rr = function(handler) {
var br = function(handler) {
var called = false;
var ready = function () {
if (called) return;
called = true;
handler();
}
if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", function(){
document.removeEventListener( "DOMContentLoaded", arguments.callee, false);
ready();}, false);
}
else if(document.attachEvent) {
if(document.documentElement.doScroll && window == window.top) {
var tryScroll = function (){
if (called) {
return;
}
try {
document.documentElement.doScroll("left");
ready();
}
catch(e) {
setTimeout(tryScroll, 10);
}
}
tryScroll();
}
document.attachEvent("onreadystatechange", function(){
if (document.readyState === "complete") {
document.detachEvent( "onreadystatechange", arguments.callee);
ready();
}
});
}
if(window.addEventListener) {
window.addEventListener('load', ready, false);
}
else if(window.attachEvent) {
window.attachEvent('onload', ready);
}
else {
window.onload=ready;
}
}
if (!rl.length) {
br(function() {
for(var i=0; i<rl.length; i++) {
rl[i]();
}
});
}
rl.push(handler);
};
rr(handler);
})(function(){
/*************************************************/
//тут код
/*************************************************/
});