Jquery vs Prototype/Scriptaculous

J

About two years ago I started learning AJAX and drag and drop.  The first project I applied it to was an existing project that was using Prototype and Scriptaculous.  So, I didn’t really have a choice as to what library I was going to use.

Two years later, I do not know Jquery all that well, but I am absolutely falling in love with.  Doing things with Jquery seem to be 10 times easier to me.

I always struggled with the each() function that I seemed to be constantly using with Prototype.  Jquery seems to understand this and simplify things for us.

In this article, I’m going to describe my top reasons why I am becoming a Jquery lover over Prototype.

  1. Set up draggable/droppables
  2. Perform AJAX call
  3. Jquery “noConflict()”
  4. Jquery is minified

Set up draggable/droppables

Below are two examples.  One example is Jquery and setting up all <li> elements to become droppable elements on my page.  The second example is Scriptaculous and setting up all <img> tags to be draggable elements.

Jquery

[code]$(“ul li”).droppable({
hoverClass: ‘droppable-hover’,
greedy: true,
tolerance: ‘pointer’,
drop: function(ev, ui) {

}
});
[/code]

Scriptaculous

[code]$A($(‘draggables’).getElementsByTagName(‘img’)).each(
function(item) {
new Draggable(
item,
{
revert: true,
ghosting: true
}
);
}
);
[/code]

As you can see there is not a lot of difference in lines of code, but it’s so much easier for me to remember Jquery’s syntax of $(“ul li”) because I use the exact syntax with every other Jquery statement I perform.  I can never remember the .each() with a function inside of that that contains an item.

Perform AJAX call

Below are four examples.  One example is Jquery perfoming a GET request and when it’s complete a custom function to perform something with the data.  The second example is simply loading the results of a GET request directly into an element.  The next two examples are similar, but for Prototype.

Jquery

[code]$.get(url, function (data) {
// do something with that data
});
[/code]

[code]$(“#someid”).load(url);
[/code]

Prototype

[code]new Ajax.Request(‘/some_url’, {
method:’get’,
onSuccess: function(transport){
var response = transport.responseText || “no response text”;
alert(“Success! \n\n” + response);
},
});
[/code]

[code]new Ajax.Request(‘/some_url’, {
method:’get’,
onSuccess: function(transport){
var response = transport.responseText || “no response text”;
$(“someid”).innerHTML(response);
},
});
[/code]

As you can tell, the first examples of each are very similar, but the second example is so much slicker in Jquery.  We simply tell it to load this page into the HTML of this element!

Jquery “noConflict()”

Below is an example of how to configure Jquery to not use the $.  This is extremely useful if you are in the process of migrating to Jquery with an existing site that uses Prototype.  If it were vice-versa it could be a very messy process!

[code]var $j = jQuery.noConflict();
[/code]

Reference: http://docs.jquery.com/Using_jQuery_with_Other_Libraries

Jquery is minified

In that first project I discussed earlier, I had spent hours and hours attempting to convert Prototype and Scriptaculous into a format that I could minify.  Unfortunately, after much pain and suffering I was unable to do it and had to rely on simply GZIPing my Javascript to increase the load time.

When downloading Jquery, they provide a production version that is already minified for you!  On top of that, if you are working with Jquery’s UI, you are able to customize the download and make it smaller if you only require a couple of the features.

Now, I know Scriptacuolous allows you to only load the libraries you need, but this process in itself is not very optimized compared to Jquery.
If you were previously on the fence about which library to use, I would suggest Jquery for the above reasons and I’m sure there are many more, but I only have so much time in one night to list them!

About the author

  • http://www.last-chaos-gold.com last chaos gold

    Great article, again. These informations are especially useful …

  • Nightfly

    A little unfair comparison.

    1. For Script.aculo.us, use this:
    $$(‘#draggables img’).each(
    instead of
    $A($(‘draggables’).getElementsByTagName(‘img’)).each(

    2. The Ajax syntax is OK for me, jQuery’s version being “sweeter” but not much more easy.

    3. Right 🙂

    4. True. But minification is not so important. gzip is. And when you use a CDN-hosted version, it doesn’t matter anyway.

  • christiano

    You may have a point with “each”, but I’m not sure that I would call Prototypes “each” a “struggle”. Also, your Ajax examples aren’t exactly fair to Prototype. I know it’s a quibble, but why use ‘some/url’ for Prototype while a short variable for jQuery: it hurts your case.
    Also, the example you do use aren’t exactly equivalent. Sure you’ve got a simpler version in jQuery typed out, but what if you want a “post” instead of “get”? You’ve included all kinds of options and fine grained controls (and some stuffing as well) in the Prototype version that you don’t have in the jQuery one:
    var response = transport.responseText || “no response text”;
    — where is your equivalent in jQuery? and what if I wanted JSON or XML, or what if I wanted some other trigger than “onSuccess”.

    In Prototype you could write a load script that would take all the options out of a regular Ajax.Request too, and only takes and url and an id as an argument, but that’s up to you.

    I like your argument for jQuery’s iteration format, but you still didn’t give prototype a fair shake on the comparison. How’s this:

    $$(’#draggables img’).each(function (item){ new Draggable( item,{ revert: true, ghosting: true }});

    not so bad, and I think that more options are better. jQuery makes things too simple sometimes.

  • http://www.last-chaos-gold.com last chaos gold

    A wonderful article…. this is just what I needed to read today. Thanks for describing the way you work and how you structure your writing projects. I’ll go read that article now.

  • http://www.samrx.com/buy-apcalis-jelly.aspx Apcalis Oral Jelly

    Great piece of article. It explains so much about the topic. I should say it is a detailed article. Talks about a variety of things – something which I never thought could exist. What I found different in your article is the way you have gone about to explain the topic in a simplistic way.

By Jamie

My Books