JavaScript Package Importer
This is an extension of an idea that Google posted on how to reduce
Mobile Gmail Startup Latency
. Essentially, the idea is to comment out any JavaScript code
that is not needed at the initial startup. When the code is required,
it can then eval'd for use by removing the block comment.
I wanted to take this idea to the next level. Combine multiple JavaScript files into one single file, leaving a JavaScript Package to be used throughout a site or application. I never really intended for this to be another "JS Loader", it just kind of happened.
The example uses a very basic library to help demonstrate this idea.
A few key features:
I wanted to take this idea to the next level. Combine multiple JavaScript files into one single file, leaving a JavaScript Package to be used throughout a site or application. I never really intended for this to be another "JS Loader", it just kind of happened.
The example uses a very basic library to help demonstrate this idea.
A few key features:
- On-Demand JavaScript Loading Parsing
- Asynchronous Loading
- Chainable Implementation
Quick How-To
<script type="text/javascript" src="/projects/javascript-package-importer/js/Importer.js">
// Require jQuery
JS.Require("/js/jquery.js",true,function(){
// Call within callback() to avoid blocking JS files that don't
// rely on jQuery to be loaded.
JS.Require("/js/jqueryui.js",true);
});
</script>
Brief Documentation
1. Load the JavaScript File/Package.
/**
* Load a standard JavaScript file, local or remote, or a specially
* formatted JavaScript Package.
*
* @param file - JavaScript Library File to load
* @param autoload - Eval() JavaScript immediately (Optional)
* @param callback - Function called when file is loaded (Optional)
*/
JS.Require(file,autoload,callback);
2. Use any of the loaded portions.
/**
* Evaluate a package, can be called at any point, before the page is
* loaded or during a specified event.
*
* @param package - Package of previously loaded JavaScript String to evaluate
* @param callback - Function called after code is eval'd (Optional)
*/
JS.Use(package,callback);