The ready sự kiện occurs after the HTML document has been loaded, while the onload sự kiện occurs later, when all nội dung (e.g. images) also has been loaded.
Đang xem: Window.onload là gì
The onload sự kiện is a standard event in the DOM, while the ready event is specific khổng lồ jQuery. The purpose of the ready event is that it should occur as early as possible after the document has loaded, so that code that adds functionality to lớn the elements in the page doesn”t have khổng lồ wait for all content to lớn load.
$(document).ready (note that it”s not document.ready, which is undefined) is a jQuery function, wrapping & providing consistency to the following events:
document.ondomcontentready / document.ondomcontentloaded – a newish sự kiện which fires when the document”s DOM is loaded (which may be some time before the images, etc. are loaded); again, slightly different in InternetExplorer and in rest of the worldand window.onload (which is implemented even in old browsers), which fires when the entire page loads (images, styles, etc.) Share Improve this answer Follow edited Atruyền thông quảng cáo 11 “16 at 21:26
Peter Mortensen 28.1k2121 gold badges9494 silver badges123123 bronze badges answered Sep 13 “10 at 6:30 Piskvor left the buildingPiskvor left the building 87.6k4343 gold badges170170 silver badges2202đôi mươi bronze badges 7 | Show 2 more comments 88$(document).ready() is a jQuery event. JQuery’s $(document).ready() method gets called as soon as the DOM is ready (which means that the browser has parsed the HTML & built the DOM tree). This allows you to run code as soon as the document is ready lớn be manipulated.
For example, if a browser supports the DOMContentLoaded event (as many non-IE browsers do), then it will fire on that sự kiện. (cảnh báo that the DOMContentLoaded sự kiện was only added to IE in IE9+.)
Two syntaxes can be used for this:
$( document ).ready(function() console.log( “ready!” ););Or the shorthvà version:
$(function() console.log( “ready!” ););Main points for $(document).ready():