Existe una muy buena documentación sobre cómo detectar si el documento se ha cargado. usando Javascript o Jquery.
Usando el Javascript nativo esto se puede lograr
if (document.readyState === "complete") {
init();
}
Esto también se puede hacer dentro del intervalo
var interval = setInterval(function() {
if(document.readyState === 'complete') {
clearInterval(interval);
init();
}
}, 100);
Por ejemplo, por Mozilla
switch (document.readyState) {
case "loading":
// The document is still loading.
break;
case "interactive":
// The document has finished loading. We can now access the DOM elements.
var span = document.createElement("span");
span.textContent = "A <span> element.";
document.body.appendChild(span);
break;
case "complete":
// The page is fully loaded.
console.log("Page is loaded completely");
break;
}
Usando Jquery Para verificar solo si DOM está listo
// A $( document ).ready() block.
$( document ).ready(function() {
console.log( "ready!" );
});
Para verificar si todos los recursos están cargados, use window.load
$( window ).load(function() {
console.log( "window loaded" );
});
document.onload=
no es molesto en.wikipedia.org/wiki/Unobtrusive_JavaScript