5 Things To Know About Javascript Libraries

Javascript libraries are relatively new, but they're already everywhere. Almost every major website has some type of library in use (Facebook - Scriptaculous, Amazon - jQuery, Apple - Scriptaculous, Google - jQuery, Digg - jQuery, blah blah blah). With the way that this trend is going, some ground rules need to be laid.

1. Be CAREFUL using a library with another library

It's like sticking two coders on the same job -- they just get in each other's way. Often, a library will have a function with the same name as another library, and the page will freak out and, according to Murphy's law, use the method that you didn't want.

Users of jQuery are can breathe a sigh of relief because it's almost completely free of this problem. The jQuery library and almost all of its plugins staying within the jQuery namespace. However, even then, jQuery uses the '$' as a shortcut for 'jQuery' and the '$' is also used by other Javascript libraries, like Prototype. Luckily, jQuery has a function called 'noConflict()' which overrides this behavior, and you can even set your own shortcut to something that won't conflict with other libraries (like '$j' for example).

However, this is bad. Just don't do it. It's like burping at the dinner table. Sticking to one, and only one, JS library will make things much simpler.

2. Learn (a little) Javascript first

Using a library without learning Javascript is like trying to cook with ingredients that you've never heard of. It's trial and error, and it usually ends up like a big jumbled mess. Javascript is EASY! It has a tiny API, non-static types, and a really simple syntax. Libraries WILL make sense if you know even a tiny bit of the JS behind it. Otherwise it will just be memorization, and will probably take more time than if you had just studied a little JS to begin with.

As icing on the cake, one you've seen how much JS code it would take to accomplish what your new jQuery one-liner whips out, you'll be able to appreciate it. You'll freak out when you see that your old AJAX call, which looked like this:

JavaScript:
  1. function ajaxFunction()
  2. {
  3. var xmlHttp;
  4. try
  5.   {
  6.   // Firefox, Opera 8.0+, Safari
  7.   xmlHttp=new XMLHttpRequest();
  8.   }
  9. catch (e)
  10.   {
  11.   // Internet Explorer
  12.   try
  13.     {
  14.     xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  15.     }
  16.   catch (e)
  17.     {
  18.     try
  19.       {
  20.       xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  21.       }
  22.     catch (e)
  23.       {
  24.       alert("Your browser does not support AJAX!");
  25.       return false;
  26.       }
  27.     }
  28.   }
  29.   }

now looks more like this:

JavaScript:
  1. $.get("page.php");

You'll get excited again. You'll have fun. Coders who have fun do a better job. Sure, appreciation of a language is not your top priority. You just want to skip the tough part and get to the good stuff. Shut up. Learn Javascript. Just do it.

3. All hail the plugins

As if the libraries weren't a big enough help themselves, most of the big boys (jQuery, mootools, YUI, Prototype) come with user-submitted plugins that do even more of the work that you didn't want to have to do yourself. One of the first mistakes a JS library newbie will make is to try and write everything with their new library, rather than searching out a plugin which probably does it better and more efficiently than you were going to anyway.

4. Don't overuse them just because they're easy

We've all been to the site that opens every link with AJAX to avoid leaving the page, displays every message with some crazy lightbox clone, has a fading slideshow everywhere you look...you know what I'm talking about. If websites were rappers, this page would be Flava Flave.

However, this is the temptation we're being faced with. Libraries let us do an incredible amount in just a few lines, and even those few lines are freakishly simple. We want to say, "This is AWESOME! Can I use it on this too?!" The answer is NO! Javascript can really be used effectively to make a website more usable, or even better looking. But when it gets to a point where you're animating everything you can get your hands on, you need to calm down. We're after usability. Your average Joe doesn't give two craps how cool looking your animated navigation menu is if he can't use it to get to the page where he's going. Use Javascript to make pages easier, and THEN cooler. Not the other way around.

5. Javascript libraries are NOT cheating!

There is a growing voice of dissent in the web design community which says that we need to be writing our own Javascript, and stop taking somebody else's code to make it adapt to our purpose. To these people I say: you're missing the point. We are web designers. Our whole job is to crank out quality websites on budget and on time. Javascript libraries save us time. Plugins save us more time. That's the bottom line.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Fark
  • Furl
  • YahooMyWeb

5 Responses to “5 Things To Know About Javascript Libraries”

  1. replicant Says:

    couldn’t agree with you more on #5; actually i agree with all of your points. great post!

  2. Mike Says:

    Thanks for the kind words.

  3. praveen Says:

    I’m new to javascript framworks. Basically I was fascinated by the mootools framework and when I tried to plunge int that I realized that there are so many other frameworks too. Now I’m confused as to what to start with.
    Which framework do you recommend??

  4. Mike Says:

    @praveen:

    For a stepping stone in to JS frameworks, jQuery seems to fit the bill pretty well. It’s rediculously easy to learn, very powerful, and seems to have one of the most active communities out there for tutorials, plugins, etc. Check it out and let me know what you think.

  5. Shashank Says:

    Yes i agree JQuery is the best. I have used YUI stuff, but Jquery is more lightweight, easy to learn and having more free plugins.

Leave a Reply