Posts Tagged ‘jquery’

Resolve conflict between jQuery and Prototype

Thursday, December 13th, 2007

I had a strange error where Prototype and jQuery were overwriting each other’s namespace. Please don’t ask me why I happen to using both - it’s just on one page, and it’s just until I work out how to duplicate my Prototype effort in JQ (I find JavaScript utterly incomprehensible and incredibly difficult to learn).

Anyway, I tried a number of tricks with loading order and including various bits of each. I couldn’t keep it working - either I had one or the other, never both. Then I read this page: Using jQuery with Other Libraries on the official jQuery developer pages. It recommends you do as such:

   <script>
     jQuery.noConflict();
 
     // Use jQuery via jQuery(...)
     jQuery(document).ready(function(){
       jQuery("div").hide();
     });
 
     // Use Prototype with $(...), etc.
     $('someid').style.display = 'none';
   script>

But I don’t have any Prototype script in HEAD. It’s all addressed through the default Rails includes. I didn’t want to start messing with that.

I implemented the above, it didn’t work but the failure was different - now Prototype was trying to work, but erroring out with prototype TypeError: Value undefined (result of expression $) is not object.

Solution: On a lark, I removed jQuery.noConflict(); and renamed all jquery-targeted $(function) shortcuts to jQuery(function):

   <script>
     jQuery(document).ready(function(){
       jQuery("div").hide();
     });
   script>
 
// in body: Ajax.new & co Rails commands

It’s a horrible nasty hack that gets around the horrible nasty problems from using two JS frameworks on the same page. Don’t copy this, I’m posting it only for your morbid curiosity. If you dare copy this horrible technique your Good Programmer’s Card is revoked. But it works!

learning jQuery

Friday, November 16th, 2007

I’ve put it off as long as I reasonably could, but it’s a fact of life for those developing web sites these days - at some point you’ll need to learn JS. I’ve managed to hack together a bit of JS in the past, and managed to put together a few bits and pieces of Prototype code, but I’ve never been even a little bit comfortable with it.

And I’ve always disliked Prototype and its companion Script.aculo.us, or whatever the fuck it’s called. Firstly, combined they’re over 100K (!!!!) which is INSANE. A javascript library must be as small and lightweight as possible! Secondly, I find their syntax weird, trying-to-be-ruby-but-not-quite, hard to grasp and frankly Not Worth Learning (TM). So, I needed a replacement.

Contenders are MooTools and JQuery. Initially I had thought mootools was the best option - it’s certainly got a nice website, with some very compelling demos. But as I looked into it further I lost confidence. You know the feeling - you begin to realise that this #{whatever} has been designed and built by someone who does not share your perspective on things. The demos, while beautiful, were opaque and uninstructive. The mootools framework ignores namespace and plays havoc with anything else you might wish to use. The documentation is a shambles, third party writings have a decided “in-group” feel to them, even the pictures of the developers had a nasty 37Signals-esque “kewl” look to them. Well, fuck that, and fuck that name as well.

Which leads me to jQuery, at 24KB (packed) the smallest (and fastest) of the frameworks.

I like a number of things about jQuery. Firstly, as I mentioned, it’s lean and mean. Seriously - fuck Prototype and its 100K download before you’ve even done anything!! In what kind of universe is that even remotely acceptable? People still use dial-up, you know! You’re looking at upwards of 20 seconds just to download the javascript file!

Anyway. Secondly, I like its selectors. They’re closer to the “metal” of CSS/DOM than any of the others. For example, here’s how you’d, say, hide

="hide">hide me!>

in jquery:

$('span.hide').hide();

I like it. $ sends to jQuery. Then you have the css selection: a span, and then its class (with a dot - an ID is selected with a #, just like CSS) - all sent, nicely, to a reasonably named function. Understandable enough.