The event ready
is triggered after the HTML document has been loaded.
onload
is only triggered when all content is loaded (including images, videos, etc.).
Notice that ready
is specific to jQuery. That is, it does not exist "natively". It is intended to run as fast as possible after loading the document, without having to wait for all the content to be loaded.
Example of document.ready
using a heavy image
$(document).ready(function(){
console.log('Ready disparado');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div>Meuconteúdo</div><imgsrc="http://joumxyzptlk.de/pics/normal/unigine/Unigine%20Valley%2010k%20(10240x5760)%20-%20000.jpg" />
Example of onload
window.onload = function(){
console.log('Onload disparado');
}
<div>Meu conteúdo</div>
<img src="http://joumxyzptlk.de/pics/normal/unigine/Unigine%20Valley%2010k%20(10240x5760)%20-%20000.jpg"/>
The DOMContentLoaded
event has the same intent as ready
.
Ex:
document.addEventListener("DOMContentLoaded", function(event) {
console.log("DOM completamente carregado e analisado");
});
for(var i=0; i< 1000000000; i++)
{}
<div>Meu conteúdo</div>