简体   繁体   中英

loading javascript dynamically, check when all javascript is loaded

I'm still learning by making my own loader; here's my progress:

<!DOCTYPE html>
<html>
<head>
    <title>test</title>

    (function( $ ){

        $.plugin = {

            loadJS: function(src, onload, onerror){
                var script   = document.createElement("script");
                script.type  = "text/javascript";
                script.src   = src;
                script.onload = onload;
                script.onerror = onerror;
                script.onreadystatechange = function () {
                    var state = this.readyState;
                    if (state === 'loaded' || state === 'complete') {
                        script.onreadystatechange = null;
                        onload();
                    }
                };
                document.body.appendChild(script);
            },

            loader: function(o) { 
                var loaded = ({js:[],css:[]});
                // http://www.crockford.com/javascript/private.html
                var that = this;
                var phase = 0;

                $.each(o["js"], function(key,src) {
                    that.loadJS(src,
                    function(){
                        loaded['js'].push(src);
                    });
                });

                console.log(loaded['js'].length)

                // IF JS ALL LOADED, this is the main problem
                if (loaded['js'].length == o["js"].length) {
                    alert('problem solved')
                    that.loadJS(o["script"]);
                };
            }
        };
    })( jQuery );
    </script>
</head>
<body>
    <script>
    $(document).ready(function() {
        $.plugin.loader({
            js: [
                '1.js', // async
                '2.js', // async
                '3.js', // async
                '4.js'  // async
            ],
            script: '5.js', // after js loaded
            debug: 1
        });          
    });
    </script>
</body>
</html>

the problem is i still dont get how stuff work in js. above the same it will load my js randomly as its async* assuming its all alert('this is x.js loaded') inside the 1-5.js

something like

// check goes randomly here
1.js or 1 js loaded
3.js    2 js loaded
2.js    3 js loaded
4.js    4 js loaded
// it should be here

so the logic is when my all js is load if (loaded['js'].length == o["js"].length) {alert('problem solved')}; should work. but it doent attach to the event somehow.

How can we check if all my js is loaded?

I ran into problems under IE 6 with a large JavaScript heavy app where occasionally external script loading was aborted without any discernable network trouble, so I ended up doing

<script src="sourcefile-1.js"></script>
...
<script src="sourcefile-n.js"></script>
<script src="last-loaded.js"></script>
<body onload="if(!window.allLoaded){/*reload*/}">...</body>

where last-loaded.js just did

window.allLoaded = true;

and where the reload code would redirect to an error page if a reload hadn't fixed the problem after a few tries.

This isn't the dynamic loader problem, but a similar approach should work with a dynamic loader as long as you can identify a point after which all external code should have loaded and you can run a very simple inlined script at that point.

Looks like your check to see if they are all loaded is being run at the end of the loader function, so it will run immediately the async calls have been started. You need to move that check part into the the callback function thats passed to the.each function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM