Js determine whether the introduction of asynchronous js file loaded

  In the normal loading process, js loads are synchronized, that is, during the loading process, the browser will load the next blocked content. This time we have to use dynamic loading, dynamic loading is asynchronous, if we use this stuff to dynamically loaded js file in behind, it is necessary to ensure that after the file has finished loading, then perform the following.

  How to determine whether js loaded? (Achieve loadScript (url, callback) asynchronous script loading, callback function is completed, required to support IE)

the callback) { // However, this embodiment is loaded before loading prevents executing the onload event trigger var _doc = document.getElementsByTagName ( 'head') [0]; // Get head element object header label. var
    
    
        
        = document.createElement script ( 'script');        // create a script tag element. 
    script.setAttribute ( 'type', 'text / JavaScript');      // set the type attribute of the script tag. = script.type 'text / JavaScript' 
    // script.async = 'the async'; 
    script.setAttribute ( 'the src', URL);                     //    script.src = URL; 
        script.setAttribute ( 'the async', 'to true' ) ; 
        _doc.appendChild (script);                           // the script tag label attached to the head, or the browser can only be completed is determined in the following IE11. 
        = = script.onreadystatechange script.onload function () {
             IF (! the this .readyState || the this== .readyState 'loaded' || the this .readyState == 'Complete' ) { 
                the console.log ( 'JS the onload' ); 
            } 
            script.onload = script.onreadystatechange = null ;      // delete event handler. 
        } 
    } 
    
    // solve the problem of clogging onload event fires, not immediately start asynchronous loading js, but only when the onload start asynchronous loading. 
    IF (window.attachEvent) 
         window.attachEvent ( 'the onload', function () {The loadScript ( 'ocrad.js', null );});
      the else 
         window.addEventListener ( ' Load ', function () {The loadScript (' ocrad. js',null);}, false);</script>
</body>
</html>

Related Links: https://www.cnblogs.com/hq233/p/7102549.html

https://www.cnblogs.com/telnetzhang/p/5827791.html

Guess you like

Origin www.cnblogs.com/7qin/p/11116730.html