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.

Advertisements

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

  1. Dear Friends,

    I hope you are doing well. I have launched a web site http://www.codegain.com and it is basically aimed C#,JAVA,VB.NET,ASP.NET,AJAX,Sql Server,Oracle,WPF,WCF and etc resources, programming help, articles, code snippet, video demonstrations and problems solving support. I would like to invite you as an author and a supporter. Looking forward to hearing from you and hope you will join with us soon.

    Please forward this email to all of your friends who are related IT. Send to us your feed about site also.

    Thank you
    RRaveen
    Founder CodeGain.com

  2. I am learning jquery,though i convince myself that jquery is better than mootools or prototype but now i am confused.Please tell me bhaia which is better.Or do you think i should learn mootools or prototype or yui and also others.

    • Hello Saiful
      Thanks for your interest. I suggest you jQuery because google, facebook, ebay like organization use jQuery. They are our model so i think jQuery will be best for us.

      Thanks
      — Saidur Rahman

  3. Thank you very much for your better solution. I have solved my problem by your code:

    jQuery.noConflict();
    var $j = jQuery;

    Thanks……..

  4. Very nice site with sinfully posts witch are very interessting to read. But one problem i have, cause i am using the safari browser and have some problems with display your blog correctly. better u check this

  5. Hi
    I am using many scripts using jquery on one page. But all mof them do not work together.
    what should I do?

  6. Hi
    I am using many scripts using jquery on one page. But all of them do not work together.
    what should I do?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s