25 Ways to improve jQuery

Jon Hobbs-Smith has written an excellent article on 25 ways to improve your jQuery.  It’s a quite well-written list of tips and tricks.  

It’s not written for super-advanced users, nor beginners, rather intermediate users. So you won’t find entry-level tips here, nor will you find advanced explanations on why you should enclose items, or the fastest way to render something to the DOM.  It’s a series of real-life, useful, in-the-trenches-kind of tips.

My favorite tip so far is tip #23: Add a JS class to your HTML attribute.  By adding a class to your HTML attribute on the javascript load, you get a CSS declarator that is valid only if JS is turned on, which allows you a very easy way to hide things if JS is turned off.

$(‘HTML’).addClass(‘JS’);  

Very slick.

The entire article is worth reading, and worth bookmarking.  Nicely done.

Share/Save/Bookmark

Washington Post

Today we’re going to be looking at the Washington Post site, www.washingtonpost.com. I do not particularly like this site, as I find it cumbersome and annoying to navigate and read.  But I do like the paper, and this is the local news, so we’ll start there.

1. Overall Structure

I’ve taken the rendered source code of the home page, and then gone through and cleaned out the code to show you the high-level structure.  We’ll delve into details elsewhere.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html>
<head>
 <title>
  washingtonpost.com - nation, world, technology and Washington area news and headlines
 </title>
</head>

<body id="hp" class="local">
<div id="container">
 <div id="skip"></div>
 <div id="hdr"></div>
 <div id="wp-nav-box"></div>
 <div id="top"></div>
 <div id="mid"></div>
 <div id="btm"></div>
 <div id="ftr"></div>
</div>
</body>

</html> 

This isn’t bad.   They ID the body, which is a good thing.  They have a set container, then identified ID’s based on what the content will be.   I would have rather seen something other than placement-related ID’s (top, mid, btm) but that’s a minor complaint. 

I’d grade this high-level overview at a solid B+.

2. Semantics

I’d rank the semantics fairly low.  Not at the high-level so much but it really falls apart in the deeper levels.

For example, they use a <span class="h1"></span> for the main, frontpage headline.  Not a <h1>, but a span.  That’s horrific.  You really have to use the actual classes there.  <span class=”h2″> is also used.  <div class=”clearboth”></div> is also used a great deal, to clear floats. 

Yes, they do use proper <ul>'s and <p> tags, so they helps them a bit.

They do use <table> tags, why I don’t know.  It’s sporadically in the code.  Maybe some legacy inert from a CMS? 

I also really dislike how many inline style declarations they use. <div align="center" style="padding-top: 5px; padding-bottom: 5px;"> is a common occurance, which is just wrong.  As many styles as they are using, they should just class this div and style it that way.

Also used a great deal is two styles; lft and rt.  I don’t like naming my classes after what they “look” like.  What if they changed the look, and all of the sudden the stuff with the “rt” (right) class now floats left in a call-out?  These types of classes aren’t very future-proof. 

3. Content

The content area, which must be put in there from a CMS, has problems on the home page.

The main headlines and content callouts are inserted via a javascript write.  This couldn’t be more of a wrong idea. 
 name:"Site",
type:"Most Clicked Articles",
updated:"9:01 a.m. ET",
content:[
{url:"http://www.washingtonpost.com/wp-dyn/content/article/2008/12/14/AR2008121402670.html?nav=hcmodule“, linkText:”Executive Pay Limits May Prove Toothless”, kicker:”", headline:”Executive Pay Limits May Prove Toothless”, subheadline:”Loophole in Bailout Provision Leaves Enforcement in Doubt”, byline:”By Amit R. Paley”, source:”Washington Post Staff Writer”, timestamp:”December 14, 2008 11:06 PM”, blurb:”Congress wanted to guarantee that the $700 billion financial bailout would limit the eye-popping pay of Wall Street executives, so lawmakers included a mechanism for reviewing executive compensation and penalizing firms that break the rules.”, slug:”execcomp15″, commentcount:”", commenturl:”"},

While you can see how they’ve split apart the stories into the set content meta-data types (Headline,  byline, blurb, etc), they’ve inserted them into the story in an incredibly non-semantic way. 

They need to update their CMS system to insert it into the page in a better way.

They also use a LOT of inline spans and inline styles. 

<span style="font-family:arial;font-size:11px;color:#000000;">&#149; </span>

That’s just wrong.  Why they don’t use that as a class, I don’t know.

4. Conclusion.

At a high level, they have an idea of where they would like to move towards.  But it falls apart quickly. 

I’m sure they have a complex CMS system, or systems that feed into this site. 

But that is no excuse.  ESPECIALLY for the home page.  The home page is just non-semantic, and there isn’t much of an excuse.  It’s a newspaper site for one of the great local newspapers in the DC area, and it’s just not friendly.

Share/Save/Bookmark

Have Google host libraries.

You should have Google host your javascript libraries for you. I, up to recently, downloaded the libraries myself, and updated them myself, thinking it was the correct thing to do.

Boy was I wrong.

Then I tried just linking straight to the Google hosted jQuery library. Better, but not ideal.

Then I ran across this article on the Google AJAX Libraries content delivery network. This is the way to go. Using this will allower better caching, decrease latency, and increase parallelism.

If you host jQuery (or another library) on your site, your user has to download it. That’s bandwidth wasted. Chances are they’ve downloaded it already from other sites. Chances are they have numerous copies of the file in their cache already. And if they haven’t, they’ll download it from a server that has bandwidth available, and is close-by (low number of hops).

But, if a browser sees multiple requests for the same Google hosted version of jQuery, it understands that these requests are for the same file. Google’s servers will then return a 304 “Not Modified” response if the file is requested again and tell the browser to cache the file for up to one year. So, even if someone is going to tons of sites using the same library, they’ll only download the file once. That’s a huge bandwidth savings.

Parallelism has to do with concurrent connections. It basically is a bit browser/techinical/geeky, but it’s faster as you have more requests at a time available using this way.

You have two options to use this script, according to Google. The most powerful and flexible way is using The most powerful way to load the libraries is by using google.load() to name a library and your prefered version.


<script type="text/javascript" src="http://www.google.com/jsapi”></script>
<script type=”text/javascript”>
  google.load(”jquery”, “1.2.6″);
 
  google.setOnLoadCallback(function() {
    // Place init code here instead of $(document).ready()
  });
</script>

What’s nice about this is that you’re not locked into specific version. You could just say “jquery”, “1.2″ and it’ll load the most up-to-date 1.2.x version. Slick.

There are extra commands you can put in there as well. For jQuery you can put in the uncompressed command. e.g., google.load("jquery", "1.2", {uncompressed:true});

Share/Save/Bookmark

Visual Event Bookmarklet

Allan Jardine has developed a bookmarklet, called Visual Event, which shows the elements on a page that have events subscribed to them, what those events are, and the function that would run when the event was triggered.

This is a great debugging tool to quickly see a high-level interactional aspect of a page.  The results come through nicely color coded and iconized, so you get a nice report.

Now Visual Event will only recogonize events added by three libraries, which is a limitation.  Those three librares are jQuery 1.2.x, YUI 2.6.x, and MooTools 1.2.x, so they’re the popular libraries at least.

This isn’t the greatest tool in the world, but it’s a new idea, which I like.  It still needs work (more libraries, better browser integration (won’t work in IE at the moment)), but it’s a good starting point. 

The code isn’t GPL at the moment, but once he’s polished it up a bit more, he will.  So if you want to help him out, please drop him a line. 

Share/Save/Bookmark