8

I am writing a very basic JavaScript library that uses the $.ajax() function of jQuery.

How should I manage this dependency? Should I instruct users of my library to include jQuery themselves? Should I use something like RequireJS or script tag insertion to load jQuery within the library? If the latter would be better, how do I do this without causing a conflict if the user is already using jQuery?

7
  • 1
    I would require someone using your library to include jQuery on their own. It is common enough and it will highlight that they need to consider any incompatibilities within their own code. Commented Apr 23, 2013 at 18:22
  • 2
    I would suggest having your library throw an exception if jQuery isn't included and a method is used that requries jQuery. Bundling jQuery with it automatically can cause much worse headaches for situations where jQuery is already included than requiring them to include it themselves. Commented Apr 23, 2013 at 18:22
  • 5
    If all your library needs is $.ajax, then just do your own XMLHttpRequests. I'm sorry, but it would be nuts to require jQuery just for XHR. Commented Apr 23, 2013 at 18:26
  • 1
    @sillylittleme What would be the downside of requiring all of jQuery? From what I understand, most end users already have a cache of jQuery anyway. Are there other components of performance I need to think about? Commented Apr 23, 2013 at 18:43
  • 1
    @FallingPlates: Just too much download. jQuery users like to go around saying that everyone already "has" jQuery, but it's not nearly that simple. A browser's cache might have jQuery, but if it does, the question is which version? and from which CDN? There are dozens of versions of jQuery hosted by at least 3 major CDNs. The browser's cache is finite. At best, it's hit or miss. XHR is a very small part. If you don't want to do it yourself, I'd suggest finding a library that just does XHR, or that is at least modular, so you can load just the part(s) you need. Commented Apr 23, 2013 at 18:48

3 Answers 3

2

I think it kinda depends if you have more dependencies, other than jQuery.

If jQuery is your only dependency, and your library doesn't really need it's own module dependency system, I wouldn't recommend RequireJS. Just check for the existence of jQuery in your library, and throw an error otherwise.

If you're however looking to make an flexible and maintainable libary, I would recommand using some module loader (like RequireJS). This also gives you the advantage of using a build system which allows you to combine and pack your library

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

Comments

1

I ended up writing my own function to fetch JSON data as silly little me recommended in the original post. Thanks to everyone who replied. The guidance on JavaScript library dependencies was very valuable, even though I went down this other route.

I used this Stack Overflow answer as a guide for writing my own function to fetch JSON. I needed to fetch the data synchronously, so I adjusted the function with the tips outlined in this other article.

In the end, my function looked like this. I hope it helps someone else who comes along.

var fetchJSON = function(path, callback) {
  var httpRequest = new XMLHttpRequest();
  httpRequest.open('GET', path, false);
  httpRequest.send();
  if (httpRequest.readyState === 4) {
    if (httpRequest.status === 200) {
      var data = JSON.parse(httpRequest.responseText);
      if (callback) callback(data);
    }
  }
}

3 Comments

Keep in mind that this won't work in older IE's if you need it too.
Thanks for the feedback, @blockhead. What part breaks compatibility? The JSON.parse call?
Just saw that my last question is answered in the answer I reference above.
0

I would recommend you to advice the users to include jquery first. If you let me choose any example, you will see that this is a really used approach (eg .net framework)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.