Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to package a JavaScript library that requires jQuery?

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?

like image 522
John Karahalis Avatar asked Apr 23 '13 18:04

John Karahalis


People also ask

What can a JavaScript library like jQuery do for you?

jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website. jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.

Can we write jQuery in JS file?

Your JavaScript file ( scripts. js ) must be included below the jQuery library in the document or it will not work. Note: If you downloaded a local copy of jQuery, save it in your js/ folder and link to it at js/jquery. min.

Where do I put jQuery code in HTML?

Step 1: Firstly, we have to open that Html file in which we want to add the jQuery using CDN. Step 2: After then, we have to place the cursor between the head tag just before the title tag. And, then we have to use the <script> tag, which specify the src attribute for adding.

How do you call a JavaScript library?

Calling a JavaScript library function is quite easy. You need to use the script tag. As almost everything we do when using jQuery reads or manipulates the document object model (DOM), we need to make sure that we start adding events etc. as soon as the DOM is ready.


1 Answers

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

like image 63
sroes Avatar answered Sep 23 '22 17:09

sroes