www.webdeveloper.com
Results 1 to 3 of 3

Thread: Dynamic script loading

Hybrid View

  1. #1
    Join Date
    Jul 2010
    Posts
    4

    Dynamic script loading

    Hello,

    I am trying to write a class that dynamically loads scripts and css files, but ran into a problem I just can't seem to solve. Below is a simplified version of the class:

    Code:
    function DynamicResources(baseUrl)
    {
        this.baseUrl = baseUrl;
    }
    
    DynamicResources.prototype.loadScript = function(file, onLoad)
    {
        var headNode = document.getElementsByTagName('head')[0];
        
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = this.baseUrl + file;
        headNode.appendChild(script);
    
        script.onload = function() { onLoad(script, file); };
    }
    
    DynamicResources.prototype.loadScripts = function(files, onLoad)
    {
        loadingFiles = files.slice();
        
        var fileLoaded = function(newNode, file) {
            // Remove from loadingFiles
            var i = 0;
            while(loadingFiles[i] !== file &&  i < loadingFiles.length) i++;
            if(i < loadingFiles.length) loadingFiles.splice(i, 1);
            // Call onLoad if done
            if(onLoad && loadingFiles.length == 0) onLoad(); 
        }  
        
        for(i in files) 
            this.loadScript(files[i], fileLoaded);
    }
    The loadScripts function should keep track of the scripts that have finished loading by removing them from the loadingFiles array. The problem is, that the changes made to the array in one call to the local function fileLoaded won't show up in the next call (no elements are ever deleted). I suspect this has something to do with scoping rules, but I can't quit get a grip on it.

    I would really appreciate any help.

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    files.slice() makes a new copy of the array.
    loadingFiles = files; would share a common array among all instances.

  3. #3
    Join Date
    Jul 2010
    Posts
    4
    Quote Originally Posted by rnd me View Post
    files.slice() makes a new copy of the array.
    loadingFiles = files; would share a common array among all instances.
    I know. I actually want a copy. Otherwise the fileLoaded callback would delete elements from the array while possibly still looping through it. All of the calls to fileLoaded should still be able to work with the same copy though.

    There is something reallly fishy about this. I eventually got it to work by changing the names of the 'onLoad' function arguments. Obviously this should not make any difference at all. Anyway, I can't afford to dwell on it for too long, but it left me confused.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles