JavaScript的窗口之间有什么区别。onload和jQuery的$(document).ready()方法?
当前回答
时间过得真快,现在是ECMAScript 2021年,人们越来越少使用IE11。相反,最多的两个事件是load和DOMContentLoaded。
DOMContentLoaded在初始HTML文档完全加载和解析后触发。
load在DOMContentLoaded和整个页面加载后触发, 等待所有相关资源完成加载。资源示例:脚本,样式表,图像和iframe等。
重点:同步脚本将暂停DOM的解析。
这两个事件都可以用来确定DOM是否可以使用。例子:
<script>
// DOM hasn't been completely parsed
document.body; // null
window.addEventListener('DOMContentLoaded', () => {
// Now safely manipulate DOM
document.querySelector('#id');
document.body.appendChild();
});
/**
* Should be used only to detect a fully-loaded page.
*
* If you just want to manipulate DOM safely, `DOMContentLoaded` is better.
*/
window.addEventListener('load', () => {
// Safely manipulate DOM too
document.links;
});
</script>
其他回答
窗口。onload是内置的JavaScript事件,但是由于它的实现在浏览器(Firefox、Internet Explorer 6、Internet Explorer 8和Opera)上有一些微妙的特点,jQuery提供了文档。ready,它将这些抽象出来,并在页面的DOM准备好后立即触发(不等待图像等)。
美元(文档)。准备(注意,这不是文档。ready(未定义)是一个jQuery函数,包装并提供以下事件的一致性:
DOMContentLoaded -一个新的事件,当文档的DOM加载时触发(这可能是在图像等加载之前的一段时间);同样,在ie浏览器和世界其他地方略有不同 和窗口。Onload(即使在旧浏览器中也实现了),当整个页面加载(图像,样式等)时触发。
该文档。ready事件发生时,HTML文档已加载,窗口。Onload事件总是稍后发生,当所有内容(图像等)都已加载。
你可以使用文档。Ready事件,如果你想在渲染过程中“早期”介入,而不等待图像加载。 如果你需要图像(或任何其他“内容”)在你的脚本“做一些事情”之前准备好,你需要等到window.onload。
例如,如果您正在实现“幻灯片显示”模式,并且需要根据图像大小执行计算,则可能希望等到window.onload。否则,您可能会遇到一些随机问题,这取决于图像加载的速度。您的脚本将与加载图像的线程同时运行。如果脚本足够长,或者服务器足够快,如果图像恰好及时到达,您可能不会注意到问题。但是最安全的做法是允许图像被加载。
文档。Ready可以是一个很好的事件,让你向用户显示一些“loading…”标志,并在窗口上显示。onload,你可以完成任何需要加载资源的脚本,然后最后删除“Loading…”标志。
例子:-
// document ready events
$(document).ready(function(){
alert("document is ready..");
})
// using JQuery
$(function(){
alert("document is ready..");
})
// window on load event
function myFunction(){
alert("window is loaded..");
}
window.onload = myFunction;
$(document).ready()是一个jQuery事件,发生在HTML文档完全加载时,而窗口。Onload事件稍后发生,当页面上的所有内容(包括图像)都加载时。
窗口。onload是DOM中的一个纯javascript事件,而$(document).ready()事件是jQuery中的一个方法。
$(document).ready()通常是jQuery的包装器,以确保所有加载的元素都能在jQuery中使用…
看看jQuery源代码,了解它是如何工作的:
jQuery.ready.promise = function( obj ) {
if ( !readyList ) {
readyList = jQuery.Deferred();
// Catch cases where $(document).ready() is called after the browser event has already occurred.
// we once tried to use readyState "interactive" here, but it caused issues like the one
// discovered by ChrisS here: http://bugs.jquery.com/ticket/12282#comment:15
if ( document.readyState === "complete" ) {
// Handle it asynchronously to allow scripts the opportunity to delay ready
setTimeout( jQuery.ready );
// Standards-based browsers support DOMContentLoaded
} else if ( document.addEventListener ) {
// Use the handy event callback
document.addEventListener( "DOMContentLoaded", completed, false );
// A fallback to window.onload, that will always work
window.addEventListener( "load", completed, false );
// If IE event model is used
} else {
// Ensure firing before onload, maybe late but safe also for iframes
document.attachEvent( "onreadystatechange", completed );
// A fallback to window.onload, that will always work
window.attachEvent( "onload", completed );
// If IE and not a frame
// continually check to see if the document is ready
var top = false;
try {
top = window.frameElement == null && document.documentElement;
} catch(e) {}
if ( top && top.doScroll ) {
(function doScrollCheck() {
if ( !jQuery.isReady ) {
try {
// Use the trick by Diego Perini
// http://javascript.nwbox.com/IEContentLoaded/
top.doScroll("left");
} catch(e) {
return setTimeout( doScrollCheck, 50 );
}
// detach all dom ready events
detach();
// and execute any waiting functions
jQuery.ready();
}
})();
}
}
}
return readyList.promise( obj );
};
jQuery.fn.ready = function( fn ) {
// Add the callback
jQuery.ready.promise().done( fn );
return this;
};
我还创建了下面的图像作为两者的快速参考:
ready事件发生在HTML文档加载之后,而onload事件发生在稍后,当所有内容(例如图像)也已加载时。
onload事件是DOM中的标准事件,而ready事件是特定于jQuery的。ready事件的目的是,它应该在文档加载后尽可能早地发生,以便向页面中的元素添加功能的代码不必等待所有内容加载完毕。
窗口。onload:普通的JavaScript事件。
文档。ready:整个HTML加载完成时的特定jQuery事件。