javascript - How to check if DOM element is fully loaded? - Stack Overflow

I'm loading really big web page with thousands of elements.How can I test if node has fully loade

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 using MutationObserver or if not supported setInterval. – 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
Add a ment  | 

2 Answers 2

Reset to default 1

say 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744904081a4600148.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信