Archive for the ‘jQuery’ Category

Older is sometimes better or why I use an old version of jQuery.

Posted on July 21st, 2011 in JavaScript, jQuery, zepto | 1 Comment »

For the last year or so I’ve been predominantly focused on mobile web applications. While I’m a big fan of jQuery, at it’s current revision of *32.18 KB (compressed and gzipped) I think it’s generally too big for the mobile web. That’s when I discovered Zepto, a jQuery-like library that features 90% of the same functionality at a weight of 4.8 KB (compressed and gzipped). Life was good again and I resumed working with iOS and Android web apps, bidding IE adieu.

Fast forward to now and I need to make some general web pages again for a project and I need to support IE once more. These pages have some light-to-moderate js functionality and though I could write a small library from scratch, I’d be a fool in wasting so much time, though minimally, I only need:

  • Cross-browser event support
  • Cross-browser ajax support

Though one does arguably write faster and more concisely (smaller file size) if one has:

  • A chainable API
  • A CSS selector engine

The answer? Use an older version of jQuery. jQuery 1.2.6 for example is 17.18 KB (compressed and gzipped). Almost half as small as the current version. It gives me all four requirements above. Every version of jQuery has been battle-hardened against the most finicky older browsers and the newest batch of browsers aren’t really causing too much trouble. I had been writing with Zepto in Chrome initially when I made the switch to test in IE. I made the switch to 1.2.6 without issue. IE and all browsers are happy.

While I would love to take credit for thinking this through, it actually happened by accident. I referenced jQuery version 1.2.6 from Google’s CDN instead of 1.6.2.

I’m sure someone’s going to comment how I’m missing some major optimizations in later versions, but after trying the project out, I already have split second performance using an old build. If I start doing crazy animations or need .live, I’ll move up some revisions. If you do plan to try this out for yourself, know that jQuery 1.3 (20.08 KB) is when Sizzle was introduced and in jQuery 1.6 a lot of the API was modified or had signature changes (nothing that’s affected me so far).

Here’s a list of some versions along with their file sizes. Check the blog for more details.
1.6.2 (32.18 KB) – misc small bugfixes
1.5 (30.12 KB) – ajax timeout and abort
1.4.2 (24.86 KB) – .delegate
1.4 (27.25 KB) – extended .live support
1.3 (20.08 KB) – Sizzle and Live
1.2.6 (17.18) – 100% faster events
1.2 (17.21 KB)

And don’t forget if you’re using Google’s CDN (a good idea), always fallback to your own version, too, just in case.

*2.6.1, I use Charles Debugging Proxy for measurements.

jQuery comes to

Posted on January 20th, 2009 in jQuery | 4 Comments »

Today the new launched and although it isn’t exactly XHTML compliant it beats the pants off of most government websites and follows along with the new President’s plans of deploying a modern communications infrastructure (it’s also using jQuery and jQuery.thickbox as noted above).

Copy on the new site explains: “ will be a central part of President Obama’s pledge to make his the most transparent and accountable administration in American history.”

Regarding Technology, Obama has already talked about:

  • protecting the openness of the internet.
  • supporting the principle of network neutrality to preserve the benefits of open competition on the internet.
  • deploying a modern communications infrastructure.
  • getting true broadband to every community in America.

Here’s looking at a tech-positive presidency!

Improve your jQuery – 25 excellent tips

Posted on December 16th, 2008 in JavaScript, jQuery | No Comments »

Here’s a nice write-up by Jon Hobbs-Smith at detailing 25 tips on using jQuery as well as regular JavaScript advice. Most of these tips probably aren’t anything new for the advanced jQuery user (Jon considers himself intermediate, I think that’s modest), however I love lists like this because there’s a treasure trove worth of experience being shared. Altruism at it’s best!

I plan on writing more entries on my own tips and tricks with JavaScript and jQuery as well as my methods of debugging in various browsers, compression techniques, etc., but in the meantime, I’ll just comment on Jon’s list:

2. Use a [jQuery] cheat sheet

I think Visual jQuery is all you need. And now that there’s live search and clickable examples it’s even better.

3. Combine all your scripts and minify them

Dead Edward’s packer is awesome for minifying, but before I do that, I like to run my code through jSlint to make sure it’s all valid. I ignore line-break errors as I don’t agree in many cases that it’s an issue.

Additionally, when I work on large JS codebases with a lot of other people who may not code as strictly as I do, I like to use JSmin on the minimal setting (cut-and-paste version here). The compression benefit is only slightly less and it preserves single linefeeds preventing JS errors where people forgot to end their statements with a semicolon. It also makes the code easier to read and debug in a production enviornment—great for the times you want the benefits of minification without the side effect of obfuscation.

5. Keep selection operations to a minimum by caching

I just wanted to comment in Ron’s example where he used:

var myList = $('.myList');

First, if I ever reference a jQuery collection in my scripts, I like to append the dollar sign character to the beginning of the variable name to signify that it is, in fact, a jQuery collection:

var $myList = $('.myList');

It’s a lot more helpful when you access that variable further down the road and don’t have to question whether it’s a jQuery wrapped set, an actual HTML element, or something else.

Secondly, whether you’re using a JS lib or not, I would never search for an HTML collection by class name alone; What the JavaScript engine is doing in the background is looping through every single element on the page which is very intensive. It would be better to find the closest parent container first or search by specific tag name(s):


var $myList = $('ul.myList'); //added benefit of knowing the type of element you'll
// receive


var $myList = $('#closest_parent_id .myList'); // searching the children of a parent
// container instead of all the elements on the page


var $myList = $('#closest_parent_id ul.myList'); // combination of both above

19. Use noconflict to rename the jquery object when using other frameworks
22. How to check if an element exists[...]

As I’ve probably mentioned before, I always like to use noConflict() in all my projects. I move jQuery to the $j namespace and save $ for a shorthand reference to document.getElementById – I think it’s what most js developers expect and if($('myDiv)) is a lot simpler to write out than if($('#myDiv).length !== 0) or even if($('#myDiv).length).

Thanks for the link Bryan!

Blogging live from jQuery Camp 2008 (@MIT in Boston)

Posted on September 28th, 2008 in JavaScript, jQuery | No Comments »

So far it’s been great! Good food, good talks. Here’s the full topic list. (scroll down to the middle of the page)

Took some vids until my camera died, more to come, once I convert ‘em and upload ‘em. Here’s the most interesting things I learned so far:


  • jQuery does do DOM caching! It listens for DOM-modified events to clear it’s internal cache.
  • That random jquery[random numbers] variable that gets added to the window object is to uniquely identify the jQ instantiation (so you can do things like run multiple instances of jQuery on the same page).
  • I knew about .end() but .andSelf()? Cool!
  • Nokia and Microsoft join the jQuery camp. Nokia will have jQuery built into their phones (they have some browser-like app environment) $M will have it in their VS/ASP.NET and all future versions of Visual Studio will come with it going foward. link link.
  • jQuery is planning to be the first library that doesn’t use any browser sniffing. They are going to use a “quirk check” method of creating some elements that aren’t added to the page that check for things like if white space gets removed when created an element that starts with a space (IE bug) and others. This way they don’t have to be constantly updating for new browsers.
  • John’s working on a cool new DOM selector API called Sizzle that’s supposed to be a lot faster. It works by search for the last element in your query up. For example $('div p'); would look for all p elements on the page that has a parent div element.
  • You can use create a namespace when binding events. For example $('#foo').bind('', func); so it’s easy to unbind later, and you can encapsulate your events for your own plugins to prevent conflicts. You can also create your own custom events like $('#foo').bind('apple', func); and trigger them using $('#foo').trigger('apple'); .
  • $('#element').data(); lets you bind data to DOM elements (see Data Cache under Internals section here.) Since you can’t really bind an object to a DOM node jQuery assigns it a random key behind the scenes which it references. This is really handly for storing all kinds of data.

jQuery UI:
- Uses the data method above A LOT.
- Lots of enhancements, most interestingly, with drag-and-drop which detects floated elements and allows you to have draggable lists within draggable lists.
- jQuery UI team also working on some experimental stuff for Webkit only (iPhone / Safari / Chrome).
- Theming jQuery UI with ThemeRoller

- I’ve been using this really old but handy site as a jQ reference: made by Yehuda Katz (also a speaker). Asking around on the conference IRC chan I found there’s a much newer / improved one at by Remy Sharp. They’re going to try to update the prior site with the latter.
- Canvas tag is pretty basic but sweet. John Resig gave a whole presentation on it since the using jQuery with Firefox plugins couldn’t make it.

Inject jQuery into any page / A better jQuery bookmarklet

Posted on July 8th, 2008 in JavaScript, jQuery | 1 Comment »

The problem

I had to find out the language codes of all supported languages in TinyMCE (this page), but unfortunately, they were only listed in the actual download link for each file. A quick sprinkle of jQuery code in my Firebug console would have returned me a list easily but this page didn’t have it – which lead me to consider making a bookmarklet to add jQuery on any page.

The solution:

My wonderful coworker Bryan told me immediately about a simple bookmarklet that does the above called jQuerify. Looking through it, I saw a few potential issues with this script and also decided I wanted something a little more personal, so I came up with my own version:


Notable differences:

  • Moved the jQuery shortcut $ to $j. I always do this on any site I work on because I find $ is used by so many other libraries and scripts that I want to minimize conflicts. Using $j also for sure tells me I’m using jQuery.
  • If $ isn’t being used it becomes a shortcut to document.getElementById. I think it’s what people naturally expect $ to do, and in my daily coding I like using $('id'); better than $j('#id')[0]; when all I want to do is return an element.
  • Added an initial check for jQuery to prevent running the script unnecessary.

The full source looks like this:

        var s=document.createElement('script');
        var z=setInterval(function(){
                $j = jQuery.noConflict();
                alert('jQuery loaded!');
	window.$=function(x){return document.getElementById(x);};

$jQuerify <-- Try me out, drag me to your tool bar.

Happy endings

Now I can just hop back over to the TinyMCE Language Pack Download page, run my bookmarklet, type the following in Firebug:

$j('#examplecontent form:first table tbody tr').each(function(c,i){
    var country = $j(this).find('td:eq(2) label').html();
    var code= $j(this).find('td:eq(1) a').attr('href').match(/(?:code=)(..)/)[1];

And the list is returned:

0 Arabic = ar
1 Bokmål, Norwegian; Norwegian Bokmål = nb
2 Bosnian = bs
3 Bulgarian = bg
4 Catalan; Valencian = ca
5 Chamorro = ch
6 Chinese = zh
7 Croatian = hr
8 Czech = cs
9 Danish = da
10 Dutch; Flemish = nl
11 English = en
12 Estonian = et
13 Finnish = fi
14 French = fr
15 German = de
16 Greek, Modern (1453-) = el
17 Hebrew = he
18 Hungarian = hu
19 Interlingua (International Auxiliary Language Association) = ia
20 Italian = it
21 Japanese = ja
22 Korean = ko
23 Latvian = lv
24 Lithuanian = lt
25 Macedonian = mk
26 Malay = ms
27 Northern Sami = se
28 Norwegian Nynorsk; Nynorsk, Norwegian = nn
29 Persian = fa
30 Polish = pl
31 Portuguese = pt
32 Romanian = ro
33 Russian = ru
34 Sardinian = sc
35 Serbian = sr
36 Sichuan Yi = ii
37 Sinhala; Sinhalese = si
38 Slovak = sk
39 Slovenian = sl
40 Spanish; Castilian = es
41 Swedish = sv
42 Tatar = tt
43 Turkish = tr
44 Twi = tw
45 Ukrainian = uk
46 Vietnamese = vi