2

I'm working on a project that contains several features, so I'm trying to organize each feature on a separated JavaScript file in such a way that the code can be loaded on demand. I want to standardize the load and execution process so I'm trying to develop a function that can load and execute the JavaScript file if it is not present in memory or just execute it if it is already loaded. One approach would be to make the function to require two parameters, the file name and the name of entry point function, then check if the script is already loaded, if it is just execute the entry point function, or if it is not, then load the script and upon the onload event execute the entry point function, this approach works fine but it requires the main module to know all the entry point function names, which in the future may become a pain in the neck, as a careless programmer may inadvertently duplicate a function name. A much cleaner and safer approach would be to name "main" all the entry point functions, and upon first execution associate that "main" to its script DOM object... more or less like this:

Loaded module:

function(){
   function main(){
      .
      . some code here
      .
   }
   Install(getThisFileName(),main);
}();

Main module:

function Load(fn){
var el,ff,i
   el = document.getElementsByTagName("script");
   ff = "http://"+window.location.host+fn;
   for(i=el.length-1;i>=0;i--){
      if(el[i] && el[i].src==ff){
         el[i].Main();
         return;
      }
    }
    el = document.createElement("script");
    el.type = "text/javascript";
    el.src = ff; 
}

function Install(ff,ep){
var el,i;
   el = document.getElementsByTagName("script");
   for(i=el.length-1;i>=0;i--){
      if(el[i] && el[i].src==ff){
         el[i].Main = ep;
         ep();
         return;
      }
   }
}

But in order to do so I need to know the name of the JavaScript file being executed, which I don't know if it is even possible. Any Ideas?

2
  • 1
    Consider using requirejs, head.js, or other script loaders. Commented Aug 15, 2013 at 17:47
  • I second @SLaks's suggestion. As someone who implemented many one-off JavaScript loaders, I can tell you it is much less painful to use someone else's work. Commented Aug 15, 2013 at 18:25

3 Answers 3

1

The easiest way might be to have included first:

var currentFile;
function getThisFileName() {
  return currentFile;
}

And at the beginning of each file at the top simply do:

currentFile = 'thisFileName.js';

This will only work however if your code is executed sequentially, once all the files are loaded currentFile will always be the name of the last file.

And alternative method would be to set currentFile at the start of each function in each file. So while that specific function is being accessed you can call your getThisFileName() and it will tell you the last file run, or the current file being run.

Unfortunately this has a bit of overhead with your code itself.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the idea... perhaps instead of a variable I can use a literal and get rid of the function call.
I took you idea two steps further, instead of a variable I used a literal so there's no need to call a function, and the literal is generated by the server at send time, so it won't be trouble if in the future some wise guy decides to rename the files. This solution works both synchronously and asynchronously. Thanks again for the idea.
0

The following will give you all the scripts as an array.

var allScripts = document.getElementsByTagName("script");

The current file will always be the last one .. so allScripts[allScripts.length-1] will give you the current javascript file. You can access the src attribute from this.

EDIT: This won't work for asynchronous implementation.

3 Comments

Using a global won't work either.; you don't know which script is running.
Good point I haven't realized... but once you analyze it, it becomes obvious. Thanks for the idea.
I like using "document.scripts" to get a full list of scripts.
0

You could globally declare an object like:

var scripts = {
    addScript : function(filename) {
        scripts[filename] = {
            loaded : true,
            executed : false
        }
    }
}

Then at the very bottom of each file call:

scripts.addScript('myFilename.js');

Then at the end of the script's execution call:

scripts['myFilename.js'].executed = true;

Then at any time you can check scripts to see if a particular file is loaded/executed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.