Home  >  Article  >  Web Front-end  >  The difference between $(document).ready() and window.onload in jquery_jquery

The difference between $(document).ready() and window.onload in jquery_jquery

WBOY
WBOYOriginal
2016-05-16 18:41:40933browse

So if a certain image or other resource takes a long time to load, the visitor will see an incomplete page, or even a script that relies on dynamically added elements will be executed before the image is loaded, resulting in a script error.
window .onload = function() { testDiv.innerHTML = "

Dynamicly created div
"; }
The solution is to wait for the DOM to be parsed, Execute our function before images and external resources are loaded. To make this implementation feasible in jQuery:

Copy code Code As follows:

//jQuery uses the dynamically created $(document).ready(function) method
$(document).ready(
function() { testDiv.innerHTML = "
Use the dynamically created $(document).ready(function) method
"; }
);
//Or easy to use Syntax:
/jQuery Use $(function) method
$(
function() { testDiv.innerHTML = "
Use $(function) Method
"; }
);
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