prototype.js / jQuery.js conflict and resolution

when you use jQuery and prototype javascript frameworks togather on the same page, it creates conflicts.

jQuery has provided a resolution to this conflict. The resolution goes as below:

Overriding the $-function:

1) you can override that default by calling jQuery.noConflict() at any point after jQuery and the other library have both loaded. For example:

 <html>
 <head>
   <script src="prototype.js"></script>
   <script src="jquery.js"></script>
   <script>
     jQuery.noConflict();

     // Use jQuery via jQuery(...)
     jQuery(document).ready(function(){
       jQuery("div").hide();
     });

     // Use Prototype with $(...), etc.
     $('someid').hide();
   </script>
 </head>
 <body></body>

 </html>

2) Additionally, there's another option. If you want to make sure that
jQuery won't conflict with another library - but you want the benefit
of a short name, you could do something like this:

 <html>
 <head>
   <script src="prototype.js"></script>
   <script src="jquery.js"></script>
   <script>
     var $j = jQuery.noConflict();

     // Use jQuery via $j(...)
     $j(document).ready(function(){
       $j("div").hide();
     });

     // Use Prototype with $(...), etc.
     $('someid').hide();
   </script>
 </head>
 <body></body>
 </html>

For more information on the above topic please refer to http://docs.jquery.com/Using_jQuery_with_Other_Libraries
About these ads

27 Responses to “prototype.js / jQuery.js conflict and resolution”

  1. Ahsan Shahzad Says:

    there was a time when i got this same problem but eventualy found. i posted my solution on this link: http://ahsangill.wordpress.com/2008/12/30/using-jquery-and-prototype-in-same-application/

    but nice to know that ur problem was solved,

    Ahsan Shahzad

  2. Filip Says:

    Hi I have a problem implementing lightbox 2 (that uses prototype.js) and Feature Content Gallery (that uses jQuery) in the same page. One excludes the other. I’m not really into js so if you can help me I would be very grateful.

    Thanks in Advance

    • samsami2u Says:

      hi filip,

      didn’t the solution provided in this post helped you out?
      if so can you please provide me with the exact error/problem you are getting.

  3. ash Says:

    it didnt work for me

  4. Abbas Dhuliawala Says:

    Hi sachin while searching stuff i came across your blog, c i am using jquery and i want to use the auto_complete_tag of symfony which gives a call to the prototype.js of symfony implicitly. both of these are getting clashed. and its saying $ is not defined. i have already used no conflict on the page where jquery is getting loaded.. Please suggest something..

    Thanks in Advance

  5. Taufiq Says:

    its great working fine on my webside.
    thanks lots

  6. Aamir Says:

    you saved at the edge, very helpful, i solved the problem at teh last minute with your short name solution.

    many thanks

  7. Lau Says:

    Right now, you are a God to me.
    Thank you!!!!!! this saved me!!

  8. Heitor Says:

    Hi. The solution we find here and in the jQuery website includes a statement that “hides” divs. This simply “hides” the content on my page. Is there a solution without having to hide divs?? Thanks

  9. priyadarshi gautam Says:

    var xmlHttp

    function loadContent()
    {

    $(“#Menu”).load(“menu.html”);

    $(“#Clients”).load(“clientslist.html”);
    }

  10. priyadarshi gautam Says:

    I am using this jquery function in website..but this code have conflicting to other .
    How to solve conflicting problem in this function ????????????????

    ————————

    var xmlHttp

    function loadContent()
    {

    $(“#Menu”).load(“menu.html”);

    $(“#Clients”).load(“clientslist.html”);
    }

  11. krishna Says:

    @samsami2u : I was struck with this issue for almost 3 days and you helped me save bread for the rest of ma life!!!

    Three cheers to you friend.

  12. jquery.min.js & prototype.js – conflict and resolution « basri.my Says:

    [...] jquery.min.js & prototype.js and this result in a conflict. After googling around I found this page citing the same problem. Still no luck after trying the suggested solution [...]

  13. Joseph O Says:

    Have two weeks on and off working on this issue. I attempted to above correction and have had no luck. I am using jquery-1.2.6.js. Is this a problem of itself? Looking for ANY help I can get so I my normal life will return.

  14. seeni Says:

    any idea about oscommers . I search a lot about the file format in oscommers and what are functionality for that. Please help me !!

  15. seeni Says:

    jquery conflict with the other .js files.

  16. Rao Says:

    Excellent boss !…

    Thnx alot…

  17. Mansimran Singh Says:

    beautiful!!
    thank you very much ! =)

  18. Kaushal Says:

    Hello Samsami2u,
    I am getting error when prototype.js & one other js both are in a same page.
    What should be the solution?

    Actually I am having a slider in a page that is using a js file named “all.js” & in that page drop-down menu is there.
    pls. see http://eagle.arvixe.com/~houselav/index.php/gallery/chanel-gallery.html (In this page “Shop” menu is having a drop-down menu).

    Now when including “all.js” file if I write jQuery.noConflict(); after including “all.js” then slider stops working & if I do not write no-conflict() after including “all.js” then drop-down menu stop coming.

    Any solution?

    Thanks.


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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 25 other followers

%d bloggers like this: