How to make jQuery work on the same page with Mootools and or Prototype?


Recently i have faced thats problem on my project. Several of my pages use both JQuery and Protoype. Also i have used Mootools some pages. JQuery this appears to be causing problems, because both libraries define a function named ‘$’.

JQuery provides a function noConflict() which relinquishes control of $ to other libraries that may be using it. So it seems like I need to go through all my pages that look like this:

src=”js/mootools.js”

src=”js/prototype.js”
src=”js/jquery.js”

and change them to look like this:

src=”js/mootools.js”

src=”js/prototype.js”
src=”/obp/js/jquery.js”
jQuery.noConflict();
var $j = jQuery;

I should then be able to use ‘$’ for Prototype and ‘$j’ (or ‘jQuery’) for JQuery. I’m not entirely happy about duplicating these 2 lines of code in every relevant page, and expect that at some point somebody is likely to forget to add them to a new page. I’d prefer to be able to do the following

  • Create a file jquery-noconflict.js which “includes” jquery.js and the 2 lines of code shown above
  • Import jquery-noconflict.js (instead of jquery.js) in all my JSP/HTML pages

An alternate solution is simply to add the 2 lines of code above to jquery.js directly, but if I do that I’ll need to remember to do it every time I upgrade JQuery.