Javascript onload事件 - 如何判断脚本是否已加载?

时间:2011-01-04 15:07:30

标签: javascript dynamic onload

如何使用javascript确定HTMLScriptElement是否已完全加载?

如何使用onload或onreadystate更改事件确定动态加载的脚本是否已完成加载 而不使用

代码如下:

   TOOL.loadScript = function (url, callback, reappend, deepSearch) {
      var head, curr, child, tempScript, alreadyAppended, queue, 
      alreadyHandled, script, handler, loadFunc; 

      head = document.getElementsByTagName("head")[0];
      tempScript = document.createElement("script");
      tempScript.src = url;
      alreadyAppended = false;

      queue = []; 

      if (deepSearch) {
         // search entire document
         queue.push(document.firstElementChild);
      }
      else { 
         // just search the head element
         queue.push(head); 
      }

      while (queue.length !== 0) { 
         curr = queue.shift();

         // add child nodes to queue
         child = curr.firstElementChild; 
         if (child !== null && child !== undefined) { 
            queue.push(child);
            child = child.nextElementSibling;
            while (child !== null && child !== undefined) { 
               queue.push(child);
               child = child.nextElementSibling;
            } 
         }

         if (curr.tagName !== null && curr.tagName !== undefined) {
            if (curr.tagName.toLowerCase() === "script" && curr.src === tempScript.src) {
               script = curr;
               alreadyAppended = true;
               break;
            }
         }
      }

      if (!alreadyAppended) { 
         script = document.createElement("script");
         script.type = "text/javascript"; 
         script.async = true; 
         script.src = url; 
      } 

      alreadyHandled = false; 

      handler = function (event) {
         console.log("handling event..."); 
         if (!alreadyHandled) { 
            if ((!event.readyState) || (event && (event.readyState === "loaded" || event.readyState === "complete"))) {  
               alreadyHandled = true; 
               callback.apply(script, [url]);
               if (loadFunc) { 
                  loadFunc.apply(script, arguments); 
               }
            }
         }
      }; 

      if (script.onreadystatechange === undefined) { 
         loadFunc = script.onload;
         script.onload = handler; 
      } 
      else { 
         loadFunc = script.onreadystatechange; 
         script.onreadystatechange = handler; 
      } 

我需要帮助。我希望即使alreadyAppeneded === true和已经加载了相同的脚本,也要触发回调函数,但前提是该脚本已完全加载。

      if (!alreadyAppended || (alreadyAppended && reappend)) { 
         head.appendChild(script); 
      } 
   };

BOTTOM LINE:如何确定脚本是否已完成加载?如果需要,请问我问题。

谢谢。

3 个答案:

答案 0 :(得分:3)

为什么不在脚本元素中添加id?在继续之前检查id是否存在....

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); 
}

答案 1 :(得分:2)

延迟实现:创建一个数组,您可以使用该数组将所有已加载脚本的源推送到其上,并在加载时将它们推送到列表中。每次都检查给定的src是否在数组中,如果是,则立即触发回调。

当你的附加但未加​​载的情况成为问题时你如何处理。如果你希望激活回调,但是你希望它在加载后触发,你可以做一个以src为键的关联数组,并将script元素作为值。从那里开始,通过包装原始内容使onload或onreadystatechange起火两次,如下所示:

var temponload = element.onreadystatechange || element.onload;
if (element.onreadystatechange === undefined)
    element.onload = function(e) { temponload(); temponload(); };
else
    element.onreadystatechange = function (e) { temponload(); temponload(); };

你有其他代码可能需要挂钩,但这应该让你开始希望。

答案 2 :(得分:1)