Before you fans of JavaScript libraries (e.g. jQuery, MooTools, Prototype/scriptaculous, etc.) form a mob and beat me to death with your coffee mugs, let me quickly state that I am a BIG fan of JS libraries. They make life much easier in most programming scenarios, BUT…
Sometimes using them for smaller JavaScript tasks and effects is simply overkill. It does not justify the loading of a whole library of 30/40/or 60kB just to use a small portion of it, especially for client-side effects.
Case in point: I wanted to add the text resizer tools to my blog posts as you see on the right hand side and I started to determine the best solution to achieve this.
I like using jQuery for its elegance and simplicity but I find myself using MooTools a lot because it accommodates the use of classic JavaScript without complaining. It also keeps things simple for me since I also code in ActionScript, so the differences are not that varied.
I started coding the text resizer code in MooTools speak as following:
window.addEvent('domready', function(){ var currentSize = 12; $('increase').addEvent('click', function(){ sizeUp(); }); $('decrease').addEvent('click', function(){ sizeDown(); }); $('txtreset').addEvent('click', function(){ resetCurrentsize(); }); }); function getCurrentsize() { return currentSize; } function setCurrentsize(newsize) { currentSize = newsize; if (document.getElementById) { $("resizeableText").setStyle('font-size', currentSize+'px'); } } function resetCurrentsize(){ currentSize = 12; if (document.getElementById) { $("resizeableText").setStyle('font-size', currentSize+'px'); } } function sizeUp(){ if (document.getElementById) { if (currentSize != 14){ currentSize++; $("resizeableText").setStyle('font-size', currentSize+'px'); } } } function sizeDown(){ if (document.getElementById) { if (currentSize != 11){ currentSize = currentSize - 1; $("resizeableText").setStyle('font-size', currentSize+'px'); } } }
On top of that I needed to load the MooTools library and I wasn’t happy with the additional footprint this implies, so I set about refactoring my code to being plain old JavaScript with no additional library to load.
Here’s how it turned out:
/* ************************************************************** * Text-resizing script * Plain JS. No Mootools, jQuery, Prototype, or any of that bulk * * Copyright (c) 2008-2009 Figo Mago. All rights reserved. * * Released under the GPL license * http://www.opensource.org/licenses/gpl-license.php * Use this script as you wish, but please leave the notice intact * * Author: Sifiso Khumalo a.k.a figomago * Author URI: http://figo.tandolin.co.za/ * **************************************************************/ var currentSize = 12; el = document.getElementById("resizeableText"); function getCurrentsize(){ return currentSize; } function setCurrentsize(newsize){ currentSize = newsize; if (document.getElementById) { el.style.cssText = "font-size:"+currentSize+"px"; } } function resetCurrentsize(){ currentSize = 12; if (typeof(preDefaultSize) != "undefined") { currentSize = preDefaultSize; } if (document.getElementById) { el.style.cssText = "font-size:"+currentSize+"px"; } } function sizeUp(){ if (document.getElementById) { if (currentSize != 14){ currentSize++; el.style.cssText = "font-size:"+currentSize+"px"; } } } function sizeDown(){ if (document.getElementById) { if (currentSize != 11){ currentSize = currentSize - 1; el.style.cssText = "font-size:"+currentSize+"px"; } } }
(For some strange reason using “currentSize–;” in the code display messes up the front page display on this blog. Substituting the longer version “currentSize = currentSize – 1;” fixes that. Either version should work fine in your code)
The only downside is the addition of adding “onClick()=appropriateFunction();” to your tags, which I can happily live with.
That’s all the JS code needed to run the text resizer on this blog. Only 1.3kb in single-line function format. I think this re-emphasises the point of using the right tools for the job at hand.
That’s quite a lot in footprint savings, wouldn’t you say?
What similar scenarios have you encountered that did not not warrant using a JS framework?
I was in a bit of a hurry so I was lazy to convert the whole text resizer bit into a WordPress plugin. I you would like the full code for the text resizer with instructions to add it to your blog, just give me a shout.
5 Responses for "Why Using A JavaScript Library May Not Be Ideal"
I agree. I will often use DOMAssistant (parts of it) when a lib. is just overkill.
http://www.domassistant.com/
cody
March 2nd, 2009 at 6:01 pm@cody Thanks for the info. DOMAssistant looks quite interesting, I will definitely give it a go.
March 2nd, 2009 at 7:03 pmI think you’re missing the point of JavaScript libraries – you are totally correct that they are overkill if you want one simple effect on your site, but what if down the road you want animation? What if you want cross-browser AJAX calls? What if you want XYZ, yadda yadda yadda, the options go on and on.
The point is – starting with a library means it’s easy to add down the road – if you add a library now, you’re set for future instances.
Of course, we can argue semantics “the code could be done differently, more efficiently as a class” etc. etc. but I think you get the point
March 3rd, 2009 at 6:01 am@keif Thank you for your input. You make a valid point. Particularly when it comes to Ajax. A lib will handle the different XHR object types for different user agents – for instance – so that you don’t have to.
Perhaps I wasn’t clear enough in the first paragraph of this post. I use JS libs all the time but in this case my objectives were a little bit different.
For my purposes I did not need all the other goodies that a JS lib has to offer, down the line I can easily migrate to a JS lib should I wish to add more boom-whizz-bang to this blog
March 3rd, 2009 at 9:49 amOh, smashing announcement close to this post. Should wish to let me know how long period of time it will take? Just because I want to do some thesis proposal or may be it will be good to determine the dissertation writing. Thank you a lot.
January 15th, 2010 at 1:51 pmLeave a reply