How to have jQuery and prototype.js in the same project

I’m a big fan of jQuery. This librarie is just the best and simplest one around. I really noticed it when I wanted to get rid of jQuery in TimmyOnTime and try to use prototype.js instead, just to be more “rails-oriented” (that’s a pretty lame excuse don’t you think?)

Why I didn’t like prototype.js

There was not a lot of javascript written for the project so I thought that getting rid of jQuery and using prototype.js would be easy. It turned out to be hell on earth. Prototype.js made me feel like I was back in the 90s writing C++. A simple click event turns out to be a awful lot of ugly code with ugly function names.

Example, if I wanted to show the content of a DIV I clicked in prototype.js, I would use things like Event.observe, bindAsEventListener, a mix of native DOM element and prototype.js element, and worst of all, an unintelligible documentation.

jQuery as a complete different way of doing it in a single easy-to-understand line :
$(“#a_div”).click(function() { alert($(this).text()); });.
As simple as that!

The problem

I wanted to use some interface element in Rails that requires prototype.js so I had to have both librairies. The problem is that there’s a conflict between them. Prototype.js doesn’t seem to give a damn about it but jQuery is nicer with you. It offers a noConflict method.

With Rails, prototype.js is the default librairie so to override this, you could do

  1. <%= javascript_include_tag "prototype", "jquery" %>
  2. <script>$j = jQuery.noConflict();</script>

This way, you will have to use $j instead of $ to call jQuery.$ would still call prototype.

Another thing you could do is to use jQuery on Rails and don’t give a damn about scriptaculous. I personally prefer to use librairies that are fully tested instead of using a “by-pass” that could break my code. It’s your choice…

  • Vishindas
    Thanks for your help... it works fine....
  • Dan Simard
    @Tobias
    Hmmm... maybe I didn't spend enough time working with prototype.js. My fault! Maybe the bad documentation has something to do with that.

    Still, I prefer jQuery for its ease of use and the selectors that are much more powerful than in prototype.js.

    I promise that I will try to use prototype a little harder next time so I will have more comparative points.

    Thanks for your good and constructive comment.
  • Tobias
    You don't have to use the bindAsEventListener for your example. It's there for the situations where you need to bind a method to a special context and then pass it as an event listener.

    You could write it like this in Prototype(*):

    $('a_div').observe('click', function(){ alert(this.text()) })

    I don't really how that should be worse than the jQuery syntax.

    (*: Prototype doesn't have a #text() method for elements right now, but that would be trivial to add.)
  • Dan Simard
    @FrankLamontagne
    I don't even understand how could someone choose prototype.js over jQuery. Just for that bindAsEventListener, prototype.js should be banned from all web applications.
  • That bindAsEventListener thing scared the hell out me as well. In general I feel that the JQuery way of doing things is more true to Javascript. prototype feels like they wanted to hide everything THAT IS javascript behind some kind of Dotnet like language.
blog comments powered by Disqus