Commits

Scott Koon committed 5c5abc0

Added an internal cache of scripts already loaded, tightened up some code.

  • Participants
  • Parent commits b8e11fb

Comments (0)

Files changed (2)

         var scriptId = id || "script" + Math.floor(Math.random() * 1234);
         var loaded = false;
         var scriptTag = null;
+        var itemsLoaded = []; //maintain a list of items we have already loaded to prevent duplicates
 
         function notLoaded(src, type) {
             var nodeList;
             var propToCheck;
-            if(type === "js") {
+            var i =0, l = itemsLoaded.length;
+            while(i < l) {
+                if(itemsLoaded[i] = scriptSrc) return false;
+            }
+
+            //last ditch effort to see if the script we are trying to load
+            // was already loaded using a static script tag.
+            if (type === "js") {
                 var nodeList = document.getElementsByTagName("script");
                 propToCheck = "src";
-            } else if (type ==="css") {
+            } else if (type === "css") {
                 var nodeList = document.getElementsByTagName("link");
                 propToCheck = "href";
             }
 
             var nodeArray = Array.prototype.slice.call(nodeList);
-
-            var len = nodeArray.length;
-            var notFound = true;
-            for(var c =0;c < len;c++) {
-                if((propToCheck in nodeArray[c]) && nodeArray[c][propToCheck] == src)
-                    notFound = false;
+            for (var c = 0, len = nodeArray.length; c < len; c++) {
+                if ((propToCheck in nodeArray[c]) && nodeArray[c][propToCheck] == src)
+                    return false;
             }
-             return (notFound);
+            return true;
 
         }
 
-        if(scriptSrc.indexOf(".js") > -1 && notLoaded(scriptSrc, "js")) {
+        if (scriptSrc.indexOf(".js") > -1 && notLoaded(scriptSrc, "js")) {
             scriptTag = createScriptElement(scriptSrc, scriptId);
-        };
+        }
 
-        if(scriptSrc.indexOf(".css") > -1 && notLoaded(scriptSrc, "css")) {
+
+        if (scriptSrc.indexOf(".css") > -1 && notLoaded(scriptSrc, "css")) {
             scriptTag = createCssElement(scriptSrc, scriptId);
-        };
-        
+        }
+
+
         scriptTag.onload = function() {
             loaded = true;
-        };
+        }
+
         scriptTag.onreadystatechange = function() {
             if (!loaded) {
                 if (this.readystate == "loaded" || this.readystate == "completed") {
 
         var id = window.setInterval(function() {
             if (loaded) {
+                itemsLoaded.push(scriptSrc);
                 makeCallback();
             }
-        }, 0);
-    };
+        }, 0)
+    }
 
     function createScriptElement(scriptSrc, scriptId) {
         var scriptTag = document.createElement("script");
     }
 
     return (window.bootstrap = window.$b = bootstrap);
-}());
+}())
     return (scripts.length==5);
 }
         , "Are all 5 scripts loaded?"
-        , "All 5 scripts are loaded.")
+        , "All 5 scripts are not loaded.")
 testSuite.add(function() {
     return ( document.getElementById("test") != null )
 }
     var count = 0;
     var l = nl.length;
     for(var c =0;c<l;c++) {
-        if("src" in nl[c] && nl[c].src == "test.js")
+        if("src" in nl[c] && nl[c].src.indexOf("test.js") > -1)
             count++;
     }
     return (count==1);
 }
         , "Was test.js loaded twice?"
-        , "test.js was only loaded once");
+        , "test.js was loaded more than once");