1

I'm working on a small JS library that requires jQuery and Raphael. I've documented this fact in the README, but I don't love the idea that users have to manually include three JS libs to use this.

What are the pros and cons of having the library dynamically include the dependencies if it doesn't find them? Like so:

if (!window.jQuery) {
    var _my_script=document.createElement('SCRIPT');
    _my_script.type='text/javascript';
    _my_script.src='//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js?';
    document.getElementsByTagName('head')[0].appendChild(_my_script);
    console.log("Loaded jQuery");        
} else {
    console.log("jQuery already loaded");
}

// get Raphael if not found
if (typeof(Raphael) === "undefined") {
    var _my_script=document.createElement('SCRIPT');
    _my_script.type='text/javascript';
    _my_script.src='//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js?';
    document.getElementsByTagName('head')[0].appendChild(_my_script);
    console.log("Loaded Raphael");        
} else {
    console.log("Raphael already loaded");
}

Kosher?

2 Answers 2

1

I personally don't know any js library that automatically download "external" dependencies and I don't think it's a good idea in a normal case.

Expressing clearly required libraries and minimal versions should be enough and that's how all js libraries I know work.

Libraries like jQuery can be downloaded from CDN, locally, etc so forcing a source and version for everyone would make no sense.

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

1 Comment

Thanks! Bold solid answers.
1

I know that highly popular libraries like backbone.js do not do this.

I think it is better to force the user to do this, so they understand how the library is working.

In general, there is usually some sort of resource loader available, that will handle adding resources. If you do this separately it kind of splinters a grouping of functionality that hopefully the end user is using.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.