我有点搞不懂你说的页面加载完成了,“ DOM 加载”还是“内容加载”?在 html 页面中,加载可以在两个类型事件之后触发事件。
DOM load: 它确保从头到尾加载整个 DOM 树。但不确保加载引用内容。假设您通过 img标记添加了图像,因此此事件确保加载了所有的 img,但没有正确加载或未加载的图像。为了得到这个事件,你应该这样写:
document.addEventListener('DOMContentLoaded', function() {
// your code here
}, false);
或者使用 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() {...})
或者使用 jQuery:
$(window).on('load', function() {
console.log('All assets are loaded')
})
function getDocReadyPromise()
{
function promiseDocReady(resolve)
{
function checkDocReady()
{
if (document.readyState === "complete")
{
clearInterval(intervalDocReady);
resolve();
}
}
var intervalDocReady = setInterval(checkDocReady, 10);
}
return new Promise(promiseDocReady);
}