The event ready
is fired after the HTML document has been loaded.
The onload
is only fired when whole the content is loaded (including images, videos, etc).
Note that the ready
is jQuery specific. That is, it does not exist "natively". His intention is to execute something as quickly 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>Meu conteúdo</div>
<img src="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 event DOMContentLoaded
has the same intention of 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>
take a look here: http://stackoverflow.com/questions/8396407/jquery-what-are-differences-between-document-ready-andwindow-load
– Jeferson Leonardo
@Jefersonleonardo doesn’t want to try an answer?
– Marconi