How to Auto Include a Javascript File | 2
[previous] |
How to Auto Include a Javascript File
Now that we have everything we need, we'll piece it together. We're going to create a "fake" function for each function that we want to auto-load. When these "fake" functions are called, they will load the real file and then re-execute the real function.
for(var fileName in autoIncludeFunctions){ var functionNames = autoIncludeFunctions[fileName]; for(var i=0; i<functionNames.length; i++){ var functionName = functionNames[i]; window[functionName] = function(){ loadScript(fileName, functionNames); var execCode = functionName+'( '; for(var i=0; i<arguments.length; i++) execCode+='arguments['+i+'],'; execCode = execCode.substr(0, execCode.length-1)+')'; return eval(execCode); } } }
The first part is relatively simple. We are looping through our list of files, one at a time, and then looping through each function in those files. Let's break down the inner code, so that it's a bit easier to understand:
window[functionName] = function(){ loadScript(fileName, functionNames);
Here, we're using a global setting for our new "fake" function. We use the window object so that this function will be available throught the code. Inside of this function, we're calling the loadScript function that we previously created. After this is called, our fake global function is replaced with the real one, and we're halfway home. We now have to call the real function with all of the parameters passed to the fake one.
var execCode = functionName+'( '; for(var i=0; i<arguments.length; i++) execCode+='arguments['+i+'],'; execCode = execCode.substr(0, execCode.length-1)+');';
We now generate a string with which to call our real function. If you didn't already know, the arguments object contains all of the parameters passed into a function. We loop through the arguments object so that we can pass each and every parameter sent to this fake function on to the real one. At the end, this string will look something like "functionName(arguments[0], arguments[1], arguments[2]);".
return eval(execCode);
Now that we have a string containing the proper call to our real function, we just have to execute it and we're done! Our real function has now replaced the fake one so that we don't have to go through all of this again, and it's been called. Any value that the real function would return gets sent to the original calling code as it should.
Things to Look Out For
If you are implementing this code, you might want to take the following into account:
- As briefly mentioned, any scripts that are loaded must be on the same server. Using this method, you can not load a script from www.some_other_site.com from www.your_site.com.
- When the auto load code is initiated, there will be a pause for the end user. Depending on the size of the file being downloaded, the speed of the user's connection and other variables, this pause can be anywhere from a few ms to a few seconds or more. During this time, the page will appear to be locked to your user. If you included this script the normal way, you would have the same delay, but it would occur when the page was loading.
- Any functions in a script file that are not defined in the autoIncludeFunctions variable will not be globally accessible. So if your file 'file1.js' has 20 functions in it, in the above example only three of them will be available for use. One solution to this is to define all of your functions in the javascript as "window.functionName = function(){" instead of "function functionName(){".
- This code has the potential to replace existing functions with the same names. You might consider adding a check for this by checking to see if window[functionName] before replacing it. If you don't do a function check, it's suggested that you run this code before including other js files, in case a function might be overridden.
If you want to see this concept in more detail, take a look at my general.js file. It's not implemented exactly as portrayed here, but it's fairly close, and is a bit more robust.
Conclusion
You now have the ability to access any JavaScript file you've ever created, all from one script. The examples in this article are straightforward, but it shouldn't be hard to modify this code to add validation to see if the file exists, prevent the code from overriding other functions, and any other features you may need.
About the Author
Mark Kahn is a Web Developer and DBA. Mark can be contacted through his website, https://www.jslibrary.org.
[previous] |
Created: June 5, 2003
Revised: March 1, 2006
URL: https://webreference.com/programming/javascript/mk/1