Loading Javascript Dynamically and how to check if the script exists

后端 未结 9 555

I am using the following technique to load up Javascript dynamically:

var script = document.createElement(\"script\");
script.type = \"text/javascript\";
scr         


        
相关标签:
9条回答
  • 2020-12-01 17:03

    Recently in my vue.js project I tried to something like this, I am using es6 so make sure you have the setup. This is just vanilla javascript, so this should run without any issue.

    function handleLoad() {
      // on scirpt loaded logic goes here
      ...
    };
    
    function handleLoadError(yourScript) {
      // on scirpt loading error logic goes here
      ...
    
      // remove the script element from DOM if it has some error
      document.head.removeChild(yourScript);
    };
    
    function generatePan(token) {
      // if script does not exist only then append script to DOM
      if (!document.getElementById('your-script')) {
        const yourScript = document.createElement('script');
        yourScript.setAttribute('src', 'https://your-script.js');
        yourScript.setAttribute('id', 'your-script');
        yourScript.async = true;
        yourScript.addEventListener('load', () => handleLoad(), false);
        yourScript.addEventListener('error', () => handleLoadError(yourScript), false);
    
        document.head.appendChild(yourScript);
      } else {
        // runs if script is already loaded DOM
        handleLoad();
      }
    };
    

    Also, please check this link, even this may help.

    0 讨论(0)
  • 2020-12-01 17:06

    Listening for events on script elements is not considered reliable (Source). One option that comes to mind is to use setTimeout() to poll for a variable that you expect to be defined in your external script. After x seconds, you could timeout your poll and consider the script as broken.

    External Script: file.js:

    var MyLibrary = { };
    

    Main document:

    var poll;
    var timeout = 100; // 10 seconds timeout
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'file.js';
    document.body.appendChild(script);
    
    poll = function () {
      setTimeout(function () {
        timeout--;
        if (typeof MyLibrary !== 'undefined') {
          // External file loaded
        }
        else if (timeout > 0) {
          poll();
        }
        else {
          // External library failed to load
        }
      }, 100);
    };
    
    poll();
    
    0 讨论(0)
  • 2020-12-01 17:08
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "file.js";
    

    You need to add a callback for this script.

    1st: Create the call back:

    function callbackFn(callbackArgs) = {
           console.log("script is loaded with the arguments below");
           console.log(callbackArgs);
        }
    

    2nd: Add an event listener to the script. Both Firefox and Chrome support onload event so you can use it like this:

    script.onload = callbackFn();
    

    For IE... You can add an event listener for a state change like this:

    script.onreadystatechange = function() {
        if ( this.readyState != "loaded" ) return;
        callbackFn();
        }
    

    Last: Append the script to the body like you used to do.

    document.body.appendChild(script);
    

    For further information, please refer to this acticle.

    0 讨论(0)
提交回复
热议问题