Commits

Scott Koon committed f59ac7d

Finally fixed duplicate problem and problem with running the test runner in the callback.

Comments (0)

Files changed (3)

 
 
 var bootstrap = (function() {
+    var itemsLoaded = []; //maintain a list of items we have already loaded to prevent duplicates
     var bootstrap = function(scriptSrc, id, callback) {
         var head = document.head || document.getElementsByTagName("head")[0];
         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;
-            var i =0, l = itemsLoaded.length;
-            while(i < l) {
-                if(itemsLoaded[i] = scriptSrc) return false;
+            var i = 0, l = itemsLoaded.length;
+            while (i < l) {
+                if (itemsLoaded[i] == scriptSrc) return false;
+                i++
             }
 
             //last ditch effort to see if the script we are trying to load
         }
 
         if (scriptSrc.indexOf(".js") > -1 && notLoaded(scriptSrc, "js")) {
+            itemsLoaded.push(scriptSrc);
             scriptTag = createScriptElement(scriptSrc, scriptId);
         }
 
 
         if (scriptSrc.indexOf(".css") > -1 && notLoaded(scriptSrc, "css")) {
+            itemsLoaded.push(scriptSrc);
             scriptTag = createCssElement(scriptSrc, scriptId);
         }
 
+        if (scriptTag) {
+            scriptTag.onload = function() {
+                loaded = true;
+            }
 
-        scriptTag.onload = function() {
-            loaded = true;
-        }
-
-        scriptTag.onreadystatechange = function() {
-            if (!loaded) {
-                if (this.readystate == "loaded" || this.readystate == "completed") {
-                    loaded = true;
+            scriptTag.onreadystatechange = function() {
+                if (!loaded) {
+                    if (this.readystate == "loaded" || this.readystate == "completed") {
+                        loaded = true;
+                    }
                 }
             }
+            head.appendChild(scriptTag);
         }
-        head.appendChild(scriptTag);
 
         function makeCallback() {
             window.clearInterval(id);
 
         var id = window.setInterval(function() {
             if (loaded) {
-                itemsLoaded.push(scriptSrc);
                 makeCallback();
             }
         }, 0)

bootstrap_test.html

 <html>
 <head>
     <style type="text/css">
-        .cssLoaded { visiblity:hidden;}
+        #cssLoaded { visiblity:hidden;}
     </style>
     <title></title>
 
     var callbackCalled = false;
     $b("test2.js");
     $b("test.css");
-    $b("test.js");
-    bootstrap("test.js","test", function(){ callbackCalled = true; testSuite.run() });
+    $b("test.js","test", function(){
+        callbackCalled = true;
+        var runnerId = window.setInterval(
+        function() {
+            if(callbackCalled) {
+                window.clearInterval(runnerId);
+                testSuite.run();
+            }}, 10) 
+    });
+
+    bootstrap("test.js","test2");
 
 </script>
 </body>
 
 testSuite.add(function() {
     var scripts = document.getElementsByTagName("script");
-    return (scripts.length==5);
+    return (scripts.length==4);
 }
-        , "Are all 5 scripts loaded?"
-        , "All 5 scripts are not loaded.")
+        , "Are all 4 scripts loaded?"
+        , "All 4 scripts are not loaded.")
 testSuite.add(function() {
     return ( document.getElementById("test") != null )
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.