admin管理员组文章数量:1405295
I'm loading really big web page with thousands of elements. How can I test if node has fully loaded including it self and all it child elements but I don't want to wait for whole page to be loaded. For example lets have this page:
<html>
<head>
<script>
var cnt = 0;
var id = setInterval(function test() {
var e = document.querySelector('#content')
if (!e) return;
// how to test is "e" fully loaded ?
if (cnt == e.childNodes.length) {
clearInterval(id);
} else {
cnt = e.childNodes.length;
console.log(cnt);
}
}, 10);
</script>
</head>
<body>
<div id="content">
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div>
<!-- ... add 30k div elements -->
</div>
</body>
</html>
I'm loading really big web page with thousands of elements. How can I test if node has fully loaded including it self and all it child elements but I don't want to wait for whole page to be loaded. For example lets have this page:
<html>
<head>
<script>
var cnt = 0;
var id = setInterval(function test() {
var e = document.querySelector('#content')
if (!e) return;
// how to test is "e" fully loaded ?
if (cnt == e.childNodes.length) {
clearInterval(id);
} else {
cnt = e.childNodes.length;
console.log(cnt);
}
}, 10);
</script>
</head>
<body>
<div id="content">
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div>
<!-- ... add 30k div elements -->
</div>
</body>
</html>
This will print something like this on console:
4448
9448
14448
19448
24948
30000
Share
Improve this question
asked Mar 7, 2015 at 15:55
czvczv
611 gold badge1 silver badge2 bronze badges
3
-
1
The only thing that would e into my mind is to have a check like this
div.nextSibling != null || documentIsReady
and observer for changes in the DOM usingMutationObserver
or if not supportedsetInterval
. – t.niese Commented Mar 7, 2015 at 16:34 - Similar (not same) question stackoverflow./questions/4057236/… and answer stackoverflow./a/41176554/14824067 . – LuckyLuke Skywalker Commented Apr 22, 2022 at 9:41
- Check with a MutationObserver for when the next sibling element is loaded. When the next sibling element is loaded, then the preceeding element and its children have been implicity loaded. – Damien Commented Aug 5, 2023 at 4:52
2 Answers
Reset to default 1say you want to alert after the div#content is loaded. if you put your javascript after div's closing tag, it will run after loading all the html prior to the script.
<div id="content">
// your content
</div>
<script type="text/javascript">
alert("finished loading until this point");
</script>
I think that the load event
would be an more apropriate answer to your question.
It can be atached to an ellement and fires when everything is loaded including images and other resources.
some examples you can find here.
https://developer.mozilla/en-US/docs/Web/Events/load https://www.w3schools./jsref/event_onload.asp
but if you don't care about the resources than you might want to look at this.
https://developers.google./speed/docs/insights/rules
本文标签: javascriptHow to check if DOM element is fully loadedStack Overflow
版权声明:本文标题:javascript - How to check if DOM element is fully loaded? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744904080a2631515.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论