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.