我正在使用以下代码在页面加载后执行一些语句。
<script type="text/javascript">
window.onload = function () {
newInvite();
document.ag.src="b.jpg";
}
</script>
但是这段代码不起作用。即使有些图像或元素正在加载,该函数仍然被调用。我想要的是在页面完全加载后调用该函数。
我正在使用以下代码在页面加载后执行一些语句。
<script type="text/javascript">
window.onload = function () {
newInvite();
document.ag.src="b.jpg";
}
</script>
但是这段代码不起作用。即使有些图像或元素正在加载,该函数仍然被调用。我想要的是在页面完全加载后调用该函数。
这可能适合您:
document.addEventListener('DOMContentLoaded', function() {
// your code here
}, false);
或者,如果您熟悉jQuery的话,
$(document).ready(function(){
// your code
});
$(document).ready()
会在DOMContentLoaded事件中触发,但是这个事件在不同的浏览器中并不一致。因此,jQuery很可能会实现一些重型解决方案来支持所有浏览器。这将使使用纯JavaScript“精确”模拟这种行为变得非常困难(但当然不是不可能)。
正如Jeffrey Sweeney和J Torres所建议的那样,我认为最好在触发函数之前使用setTimeout
函数,如下所示:
setTimeout(function(){
//your code here
}, 3000);
ready
不会实现OP所请求的功能。ready
事件会在DOM加载完成后触发,而非元素加载完成后。要想在元素加载完成/渲染完成后触发事件,需使用load
事件。 - Jaime Torresload
)。 - Jaime TorressetTimeout
是个不好的选择。它依赖于页面在3秒内(或根据你选择的值可能是n秒)加载完成。如果加载时间更长,它就无法工作;如果页面加载得更快,它也需要无意义地等待。 - JJJDOMContentLoaded
事件将比window.onload
事件更早触发... - Ryan Walkerdocument.addEventListener('readystatechange', event => {
// When HTML/DOM elements are ready:
if (event.target.readyState === "interactive") { //does same as: ..addEventListener("DOMContentLoaded"..
alert("hi 1");
}
// When window loaded ( external resources are loaded too- `css`,`src`, etc...)
if (event.target.readyState === "complete") {
alert("hi 2");
}
});
$(document).ready(function() { //same as: $(function() {
alert("hi 1");
});
$(window).load(function() {
alert("hi 2");
});
注意: 不要使用下面的标记(因为它会覆盖其他相同类型的声明):
document.onreadystatechange = ...
event.target.readyState === "complete"
非常有用,谢谢!我认为这应该是正确的答案,而不是排名最高的答案。 - Teddy van Jerry我有点困惑,你所说的页面加载完成是指"DOM加载"还是"内容加载"?在一个HTML页面中,加载可以在两种类型的事件后触发。
DOM load: Which ensure the entire DOM tree loaded start to end. But not ensure load the reference content. Suppose you added images by the img
tags, so this event ensure that all the img
loaded but no the images properly loaded or not. To get this event you should write following way:
document.addEventListener('DOMContentLoaded', function() {
// your code here
}, false);
Or using jQuery:
$(document).ready(function(){
// your code
});
After DOM and Content Load: Which indicate the the DOM and Content load as well. It will ensure not only img
tag it will ensure also all images or other relative content loaded. To get this event you should write following way:
window.addEventListener('load', function() {...})
Or using jQuery:
$(window).on('load', function() {
console.log('All assets are loaded')
})
DOMContentLoaded
事件处理程序对我似乎没有任何作用,但是load
事件有。 - Brandon Benefield如果你可以使用jQuery,可以查看load。然后,您可以在元素加载完成后设置您的函数运行。
例如,考虑一个包含简单图像的页面:
<img src="book.png" alt="Book" id="book" />
事件处理程序可以绑定到图像上:
$('#book').load(function() {
// Handler for .load() called.
});
如果您需要在当前窗口加载所有元素,您可以使用:$(window).load(function () {
// run code
});
如果您不能使用jQuery,那么纯JavaScript代码的量基本相同(如果不是更少):
window.onload = function() {
// run code
};
load
方法不就是使用JavaScript将一个函数绑定到加载事件上吗?这与OP已经在做的有什么不同呢? - Alex KalickiaddEventListener
而不是绑定DOM0的onload
属性之外,它不会有任何区别。 - Alnitakload
事件本身并没有被弃用。目前附加 jQuery 事件(包括 load
和其他事件)的标准方式是 .on( "load", handler )
,如链接文档所述,并且仍然有效。 - Sebastian Simon据我所知,你最好的选择是使用
window.addEventListener('load', function() {
console.log('All assets loaded')
});
使用DOMContentLoaded
事件的#1答案是一种倒退,因为DOM将在所有资源加载之前加载。
其他答案建议使用setTimeout
,但我强烈反对,因为它完全取决于客户端设备性能和网络连接速度。如果某人处于缓慢的网络环境和/或拥有缓慢的CPU,则页面可能需要几秒钟甚至几十秒钟才能加载完成,因此您无法预测需要多长时间才能触发setTimeout
事件。
至于readystatechange
事件,在readyState
更改时触发,在MDN上指出,这仍然会在load
事件之前触发。
Complete
该状态表示加载事件即将触发。
readystatechange = 'Complete'
表示整个页面和其内容(图片、脚本、CSS)都已加载完成是可以的。虽然它在 load
事件之前,但这里并不重要。 - S.Serpooshan如果你想在HTML页面中调用JavaScript函数,请使用onload事件。当用户代理程序完成窗口或FRAMESET中所有框架的加载时,将触发onload事件。此属性可与BODY和FRAMESET元素一起使用。
<body onload="callFunction();">
....
</body>
这种方法可以处理已经加载或未加载页面的两种情况:
document.onreadystatechange = function(){
if (document.readyState === "complete") {
myFunction();
}
else {
window.onload = function () {
myFunction();
};
};
}
window.addEventListener("load", afterLoaded,false);
function afterLoaded(){
alert("after load")
}
window.onload = () => {
// run in onload
setTimeout(() => {
// onload finished.
// and execute some code here like stat performance.
}, 10)
}
$(window).bind("load", function() {
// code here });
$(window).load()
。 - Matt Hintzkewidth
是否大于0来进行测试。 - Jeffrey Sweeney