Window.onload là gì

  -  

What are the differences between JavaScript”s window.onload và jQuery”s $(document).ready() method?




Bạn đang xem: Window.onload là gì

*

*

The ready sự kiện occurs after the HTML document has been loaded, while the onload event occurs later, when all content (e.g. images) also has been loaded.quý khách hàng đã xem: Window.onload là gì

Đang xem: Window.onload là gì




Xem thêm: Trùm Bắn Cá: Game Bắn Cá Đổi Thưởng Tiền Thật, Trum Ban Ca Vang【Vip】

*

*

window.onload is the built-in JavaScript event, but as its implementation had subtle quirks across browsers (Firefox, InternetExplorer6, InternetExplorer8, và Opera), jQuery provides document.ready, which abstracts those away, and fires as soon as the page”s DOM is ready (doesn”t wait for images, etc.).

$(document).ready (note that it”s not document.ready, which is undefined) is a jQuery function, wrapping & providing consistency lớn the following events:

document.ondomcontentready / document.ondomcontentloaded – a newish event 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 world& 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 Aquảng cáo 11 “16 at 21:26




Xem thêm: Giới Thiệu Ngôn Ngữ Vhdl Là Gì ? Giới Thiệu Ngôn Ngữ Vhdl

*

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 badges220220 bronze badges 7 | Show 2 more comments 88$(document).ready() is a jQuery sự kiện. JQuery’s $(document).ready() method gets called as soon as the DOM is ready (which means that the browser has parsed the HTML and built the DOM tree). This allows you lớn 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 event. (chú ý that the DOMContentLoaded sự kiện was only added to lớn 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():

It will not wait for the images khổng lồ be loaded. Used to lớn exeđáng yêu JavaScript when the DOM is completely loaded. Put event handlers here.Can be used multiple times.Replace $ with jQuery when you receive sầu “$ is not defined.”Not used if you want to lớn manipulate images. Use $(window).load() instead.

window.onload() is a native JavaScript function. The window.onload() event fires when all the nội dung on your page has loaded, including the DOM (document object model), banner ads và images. Another difference between the two is that, while we can have more than one $(document).ready() function, we can only have one onload function.