Javascript onload event - how to tell if script is already loaded?

后端 未结 3 606
逝去的感伤
逝去的感伤 2021-01-18 12:10

How can I use javascript to determine if an HTMLScriptElement has already been fully loaded?

How can I determine if a dynamically loaded script has finished loading

3条回答
  •  情深已故
    2021-01-18 12:56

    Why not add an id to the script element? Check to see if the id exists before continuing....

    function includeJs(jsFilePath) {
    
      if (document.getElementById(jsFilePath+"_script")) {
        return;
      }
      var js = document.createElement("script");
    
      js.type = "text/javascript";
      js.id = jsFilePath+"_script";
      js.src = jsFilePath;
    
      document.body.appendChild(js); 
    }
    

提交回复
热议问题