如果DOM已加载,我有一段JS代码需要确定。我知道有执行JS代码的几种方法,当DOM加载,如:如何知道文档是否已加载
$(document).ready(function() { ... }); // Jquery version
document.body.onload = function() { ... } // Vanila JS way
我要寻找一些方法,它看起来像
function isDOMLoaded() {
// Do something to check if DOM is loaded or not
// return true/false depending upon logic
}
PS:更新(邮政的答案接受) 我碰巧看到jquery也使用相同的方法来检查DOM是否已加载。看看在Implementation of jquery.ready() here
bindReady: function() {
if (readyBound) {
return;
}
readyBound = true;
// Catch cases where $(document).ready() is called after the
// browser event has already occurred.
if (document.readyState === "complete") {
return jQuery.ready();
}
...
可能是你可以设置DOM负载的标志,检查标志,以查看是否DOM已加载 –
其实,我的javascript代码进入页面在运行时所以我不能绑定上的document.ready事件。我只需要检查Yeahman回答的一些布尔变量类型的事情。 – sachinjain024