Home  >  Article  >  Web Front-end  >  Analysis of the difference between window.onload and $(document).ready()_javascript skills

Analysis of the difference between window.onload and $(document).ready()_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:57:121093browse

The example in this article describes the difference between window.onload and $(document).ready(). Share it with everyone for your reference. The specific analysis is as follows:

window.onload is a function in Javascript, which means: wait for all content in the web page to be loaded (including images);
$(documetn).ready() can be executed after all the DOM structures in the web page have been drawn. There may be elements associated with the DOM that have not been loaded, so it is faster in comparison;

For example, let’s take a simple example:

window.onload=function(){
  alert('I am No.1');
};
window.onload=function(){
 alert('I am No.2');
}

According to the above meaning, the result can only output "I am No.2"

Replace

with:

$(document).ready(function(){
alert('I am No.1');
});
$(document).ready(function(){
alert('I am No.2');
});

Result output I am No.1 ,I am No.2

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn