My love of the JavaScript frameworks knows no bounds. Unfortunately too many developers stick to one framework without taking the time to learn the others. The more frameworks you know, the better a programmer you will be and the more money you’ll make. Let me show you how to accomplish a few more tasks using three JavaScript frameworks: MooTools, jQuery, and Dojo.
Calculate Element Dimensions and Position
Knowing not only the height and width of a dimension but it’s top/left position from an offset parent or document body can be extremely helpful when trying to animate or move a DOM element.
MooTools
var dimensions = document.id('myElement').getDimensions(); /* returns: { height: 4684, width: 1408, x: 1408, y: 4684 } */
jQuery
var myElement = jQuery('#myElement'); var position = myElement.position(); var dimensions = { height: myElement.height(), width: myElement.width(), top: position.top, left: position.left };
Dojo
var dimension = dojo.coords('myElement'); /* returns: { h: 4684, l: 0, t: 0, w: 1408, x: 0, y: 0 } */
Extend an Object
Extending an object means taking on object and merging a second objects properties into it. This is very helpful in merging default options with instance options.
MooTools
$extend(firstObject,{ anotherProperty:'anothervalue' }); //second arg is added to the first object
jQuery
jQuery.extend(firstObject,{ anotherProperty:'anothervalue' })
Dojo
dojo.mixin(firstObject,{ anotherProperty:'anothervalue' });
Stop an Event
Stopping an event is helpful when looking to execute functionality (usually an XHR request) when a link is clicked.
MooTools
$('myElement').addEvent('click',function(e) { e.stop(); });
jQuery
$('#myElement').click(function(e){ event.stopPropagation(); e.preventDefault(); // (no internal method that does both) });
Dojo
dojo.connect(dojo.byId('myElement'),'onclick',function(e) { dojo.stopEvent(e); });
Load Content into an Element
Sure we can create an XHR request manually to load content into an element but why do that when your favorite lirbary can do that work for you?
MooTools
document.id('myElement').load('ajax/script.html');
jQuery
jQuery('#myElement').load('ajax/script.html');
Dojo
//as found on Pete Higgins' website: //http://higginsforpresident.net/2009/12/140-characters-of-awesome/ dojo.extend(dojo.NodeList, { grab: function(url){ dojo.xhr('GET', { url:url }) .addCallback(this, function(response){ this.addContent(response, 'only'); }); return this; } }); dojo.query('#myElement').grab('header.html');
Get and Set HTML Content
Getting and setting HTML is a frequent JavaScript operation…yet each library handles it a bit differently.
MooTools
//get var html = document.id('myElement').get('html'); //set document.id('myElement').set('html','Hello!');
jQuery
//get var html = jQuery('#myElement').html(); //set jQuery('#myElement').html('Hello!');
Dojo
//get var html = dojo.byId('myElement').innerHTML //set dojo.byId('myElement').innerHTML = 'Hello!';
Use Element Storage
Element data storage is important because it allows you to store settings within the element itself — perfect for defeating scope and context issues.
MooTools
//set document.id('myElement').store('key','value'); //get var value = document.id('myElement').retrieve('key');
jQuery
//set jQuery('#myElement').data('key','value'); //get var value = jQuery('#myElement').data('key');
Dojo
//set dojo.attr('myElement','data-key','value'); //get dojo.attr('myElement','data-key');
There you are — more proof that the toolkits are one in the same, all except the syntax. Do yourself a favor and learn more than one JavaScript framework — you’ll be better for it!
Read the full article at: Accomplishing Common Tasks Using MooTools, jQuery, and Dojo III
Hosted by Media Temple, domain from Name.com.