Connect With Us
Visit Us
250 West Nyack Road, Suite #200 West Nyack, NY 10994
Get Directions
Call Us Toll Free
877-GO-RUSTY
877-467-8789
Telephone
845-369-6869
Fax
845-228-8177
Departments
Departments
Departments
SUBSCRIBE TO NEWSCONTACT US
Prototype vs jQueryBefore I joined RustyBrick I had been coding basic JavaScript functionality into websites mostly "by hand", in other words, without using a well defined third party package. The exception is the Yahoo! User Interface (YUI) which assisted me with AJAX style programming. Then, thanks to the RustyBrick team, I was introduced to the Prototype JS library which provides all sorts of user interface development shortcuts and cross browser compatibility assistance. Now I wouldn't even dream of coding another website without it (or a similar packaged library as you'll soon see).
Everything was fine until I was asked to enable Google's javascript based Virtual Keyboard gadget, with newly added Hebrew support, on an upcoming project. We had already written a lot of Prototype JS functionality into the site when I was hit with an incompatibility between the virtual keyboard and the Prototype JS library. While this was most likely an oversight by Google, it also represents a flaw and feature of the library -- permanently augmenting native JavaScript objects with it's own methods.
After seeing examples of what jQuery had to offer, along with solid documentation and examples, I decided to start using jQuery in future projects, whenever possible. After a month of using it I can offer some comparisons.
To give a quick example of Prototype, lets say I want to color each paragraph red upon clicking a link inside of that paragraph, and let's assume the paragraphs are coming from a database so we don't know how many there will be. Here is how to do that with Prototype:
<!-- HTML -->
<div>
<p>Lorem ipsum <a href="#">Click Me</a></p>
<p><a href="#">Click Me</a></p>
<p><a href="#">Click Me</a> Lorem ipsum </p>
...
</div>
<script type="text/javascript">
$$("div p a").each( function(element){
element.observe("click",function(pEvent){
// 'this' refers to the DOM element on which the event occurred.
$(this).up("p").setStyle({backgroundColor:"red"});
} );
} );
</script>
To script the same thing in jQuery you'd do this:
<script type="text/javascript">
$("div p a").bind("click",function(){
// 'this' refers to the DOM element on which the event occurred.
$(this).parent().css("background-color","red");
} );
</script>
Live example:
The differences in this small example are as follows:
Because the use of jQuery is centered around using CSS selectors in general, not just element IDs, I found myself not worrying about assigning so many unique id attributes to elements all over the page server-side, especially if the HTML elements are generated dynamically by database content (which would then require a server-based increasing index, like myelement_1, myelement_2, etc). I can just assign a unique id to a container somewhere and then easily work on elements within that div using CSS selectors like $("#mydiv ul li").doStuff(). This turns out to be a cleaner and faster way to code and results in less cooperation between the client and server.
Other examples of what you can do quickly with ALL the stuff returned from $():
Other cool shortcuts I've found with jQuery:
It's not just the core library that is important -- while Script.aculo.us goes with Prototype to provide animation effects and other UI shortcuts, jQuery UI goes with jQuery. I've found jQuery's UI library and other third party addons to be much faster and simpler than that of Prototypes. For example, jQuery UI has a wonderful draggable/droppable extension that allows you to quickly specify which things can be dragged, where they may be dropped, and dynamically change these rules at any time, sometimes simply by adding or removing a CSS class name to or from the element or spot. Also, jQuery Tools has proved to be a very valuable add on for tabs, tooltips, and overlay handling.
jQuery also strives to be optimized, providing minified/compact/combined versions which results in speedy load times.
Prototype JS may prevent headaches, but jQuery prevents even more headaches. I will highly recommend it to all JavaScript developers!
250 West Nyack Road, Suite #200 West Nyack, NY 10994
Get Directions
877-GO-RUSTY
877-467-8789
845-369-6869
845-228-8177
1 COMMENT