I wrote JavaScript library to use FileSaver.js and its associated libraries. However, I don't want to always load FileSaver.js whenever someone wants to use my library. And I don't want to force them to load all the various FileSaver related JavaScript libraries with script
tags themselves (or even load one of mine which would do that).
Instead, what I'd prefer is something like this. When they call my createImage
function, it first does the following:
function createImage(image, name) {
if (typeof(saveAs) !== 'function') {
var element = document.createElement('script');
element.async = false;
element.src = 'FileSaver.js';
element.type = 'text/javascript';
(document.getElementsByTagName('head')[0]||document.body).appendChild(element);
}
// now do the saveImage code
}
Problem is, after the above, the saveAs
function is still not defined. It's only after my createImage
completes is the saveAs
function finally defined.
the Holistic solution is to use a module system. AMD is (in-my-just-an-observation-please-dont-start-a-holy-war-opinion) probably the most commonly used system for browser async code loading. AMD is just a spec, but something like require.js is a very popular tool for using AMD modules.
The idea being that you can define dependencies between your modules, and require.js will go fetch them if need be. The general idea is to mimic the import/namespace functionality of other languages (like java, C#, or python). "code sharing" i think is the term?
simply put you have all your code in a callback function that runs once the dependencies are loaded, so you can be sure the needed objects and methods are present.
update 2015
just an addendum. while the info above is still correct, front end code management is moving quickly toward solutions like Webpack and Browserify, which bundle and concatenate code of any module type and both have dynamic code loading capabilities (webpack calls this code splitting). That coupled with the exponential growth of npm for dependency management is beginning to make AMD less relevant.
Alright, what you need to do is listen for the script to finish loading. Unfortunately there are some bugs with this code for ie<7.
This is the way Mootools Asset.javascript
loads scripts and calls a callback when its complete:
var loadScript = function (source, properties) {
properties || (properties = {});
var script = document.createElement('script');
script.async = true;
script.src = source;
script.type = 'text/javascript';
var doc = properties.document || document, load = properties.onload || properties.onLoad;
return delete properties.onload, delete properties.onLoad, delete properties.document,
load && (script.addEventListener ? script.addEventListener("load", load) : script.attachEvent("readystatechange", function() {
[ "loaded", "complete" ].indexOf(this.readyState) >= 0 && load.call(this);
})), script.set(properties).appendChild(doc.head);
}
Now in loadImage
you can load the file library as follows:
function createImage(image, name) {
function createImg() {
// now do the saveImage code
}
if (typeof(saveAs) !== 'function') {
loadScript("FileSaver.js", {onLoad: createImg});//load library
}
else {
createImg();
}
}
Should work on most browsers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With