Bundling javascript with a library

Asked by At

I've written some JS snippets that webmasters are required to insert into their website to enable various features.

My code depends on jQuery.

My questions are:

  • What's the best way of bundling jQuery with my code, instead of the normal <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>, which seems like a bad idea.
  • How can I make sure any jQuery I import doesn't interfere with any jQuery they might already have on their site?

1 Answers

Meziane On

Look at this eaxmple:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
        <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.18/jquery-ui.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

   var $x = jQuery.noConflict();
   alert("Version: "+$x.fn.jquery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
   var $y = jQuery.noConflict();
   alert("Version: "+$y.fn.jquery);
        alert("First Version: " + $x.fn.jquery + ", Second Version: " + $y.fn.jquery);

try to set a "namespace" for your jquery and use it in your scripts