Why jQuery is The Answer
There are dozens of JavaScript libraries out there and god, I tried them all. And I have to say: No library solves problems in a nicer way than jQuery.
What jQuery looks like
The most important part regarding JavaScript libraries in my eye is how verbose they are. I don't want to have cryptic constructs but I also don't want to type too much. Especially since it's pretty easy to create huge files and I don't want my application to load hundreds of kilobytes of JavaScript to the client's browser.
So here a pretty nice example for jQuery that adds an anchor to all headlines:
document.ready(function() { for (var i = 0; i <= 6; i++) $('h' + i + ' [@id]').each(function() { $('<a class="anchorlink">\u00B6</a>'). attr('href', '#' + this.id). attr('title', 'Permalink to this headline'). appendTo(this); }); });
This is beautyful code in my mind. For several reasons. For one I can simply query elements by a mixture of XPath and CSS syntax. Then I can iterate over all result elements by passing an anonymous function to the each method. I don't even have to give it an variable name because jQuery automatically binds the value to this. Furthermore I can use $() to create an element and use the chaining possibilities to add attributes and append it to the headline in one go. Gorgeous!
Doing Things Right
I like cherrypicking a lot. Just combine the best of different libraries to do what you want. And JavaScript allows you to do that. Unlike PHP it's possible to have multiple namespaces (by adding objects to window) and binding values to that. jQuery uses that. It just creates one global objects and plays nice with others. The default alias from jQuery to $ is completely optional, thus you can combine jQuery with many other libraries without triggering namespace clashes etc.
It doesn't patch a single builtin. That's awesome. Also jQuery includes basic support for effects and nice event support. While the library itself is quite big with 57KB, the compressed version is only 19KB big.
It's also darn fun to work with. I don't want to miss this and firebug any more :-)
Plugins
jQuery has a list of plugins in the wiki and that page is really awesome long :-) From Since jQuery was developed in an extensible way plugins integrate nicely into the jQuery core. For example if you have interface loaded you can do this:
document.ready(function() { $('#message').click(function() { $(this).Fold(500, 30); }); });
This will fold the element with id message once it's clicked.
What I don't like about Others
- prototype.js
Prototype, the default answer you get these days by any rails developer. It's one of the first flashy javascript libraries but it sucks for many reasons. Until the most recent version it was nearly impossible to find a good documentation of it. Also the way you work with the library is too rubysh. That's not bad if you are a rails developer but it makes no sense outside the rails environment. JavaScript itself behaves a lot more like Python or LISP due to first class functions.
It also includes dozens of functions I really don't use or want to use. So why do I have to add those to my requirements too? A huge stdlib makes sense for an offline language but clearly not for javascript.
- AJS
- We used it in pocoo before we discovered jQuery. Really a nice library but the lack of XPath support is a real show stopper over jQuery. If you don't need chainability or XPath you could go with this library. It provides some nice things like forceArray :-)
- mootools
- Awesome library but I still like jQuery better. If there wasn't jQuery I would use Mootools :-) It's also a bit too flashy for my needs. I want a non sucking javascript and not blinking elements everywhere ^^
- mochikit
- Was a nice library some years ago but it's not lightweight any more.
- dojo
- Awesome but huge library. The API sucks in comparison with jQuery but it provides stunning stuff. Especially regarding vector graphics. But it's huge...
Conclusion
jQuery is really the answer. For my requirements :-) Maybe you have similar and don't know jQuery so far, give it a try. It's more than fun to work with.
I know that there are many other libraries out there but most of them have different views of some things (especially patching prototypes of public objects) which are not compatible with my point of view. This doesn't mean that those libraries are bad. But they just don't work that well for me.
Nice! but i'm sticking with mootools.
— cssProdigy on Sunday, January 18, 2009 15:18 #
Learning javascript is the answer.
— dave on Sunday, May 3, 2009 19:45 #
"The most important part regarding JavaScript libraries in my eye is how verbose they are. I don't want to have cryptic constructs but I also don't want to type too much. Especially since it's pretty easy to create huge files and I don't want my application to load hundreds of kilobytes of JavaScript to the client's browser."
If you don't want to "type too much", look into macros. And the way to avoid "hundreds of kilobytes of JavaScript" (sic) is to start with 50K+?
jQuery is not the answer to anything in browser scripting and you don't seem to know what the pertinent questions are anyway. Your example of "beautiful code" betrays your misconceptions. Careful with that attr, Armin.
— David Mark on Saturday, May 16, 2009 19:44 #
I would agree with dave. It looks like you need to learn how to write JavaScript itself instead of relying on the framework.
It's pretty funny, though, that you praise moo while dissing prototype. They're so similar it hurts.
The truth is, they're all good if you know how to write JavaScript.
— Ouch on Wednesday, July 15, 2009 4:29 #
I really cannot stand working with those libraries... I mean not only is there a layer of abstraction to deal with... as soon as you get into anybody elses work which is an extention of it, it just blows right out of proportion...
I mean... in some cases you will find they even change the behaviour of javascript objects, extending their behaviors and such... to the point where other scripts break...
I can't stand them... I would rather code from scratch... (with of course the exception of the occasional library or two which solves the cross compatibility issues when calling properties of the document)
— James on Wednesday, November 4, 2009 13:42 #