Updates – BASIC jquery ui tabs rotate documentation, a note on nodejs hosting, and a note on the re-design

nodejs, jquery ui tab rotate, and re-design. Just a few quick notes...
-
I'm actively working on documentation for the jquery ui tab rotation plugin. I've (finally) got a very basic working example up.
The plugin is stupidly easy to use:
$("#tabbedElement").tabs().tabs("rotate", 4000, true);
Note that you MUST first call tabs() before you can add the rotation with .tabs("rotate", [ms], [rotate]). Also note that as of right now none of those params are optional! You can also call .tabs("pause") and .tabs("unpause") to start and stop the rotation. You can instantiate a rotating element that starts as "paused" by passing in false for the [rotate] param.We use this plugin at FinishLine.com, and we are always on the most recent stable release of both jquery and jquery UI, which means that this plugin is nearly always guaranteed to work with the newest version of both. I'll be posting updates to this blog whenever there is anything to report. The plugin's official "homepage" is right here.
-
Node.JS is a server side implementation of JavaScript. It is basically PHP or ASP or [name your server side language of choice here] but with JavaScript. This fulfills Knuth's 3rd law, which states that any code that can be written will eventually be written in JavaScript, which fulfills Knuth's 4th law which states that Knuth's 3rd law will change languages every 10 years. In case you were wondering, Knuth's 2nd law is that a 12% improvement is easily obtainable and should not be marginalized, which compliments while nearly contradicting Knuth's 1st law, which states that premature optimization is the root of all evil.
ANYWAYS, I'm working on something FUN in node.js. If it works out, it's gonna be BIG. If not, you'll never hear about it again, lol. But that's not what I want to talk about. This is a quick note to make mention of the fact that if you are looking for a node.js server, they are out there. You don't have to roll your own. Check out no.de, or this stackoverflow question or this page on the project's wiki for hosting options.
-
And last but not least, you may notice that I have re-designed the blog. I'm now using a PREMIUM wordpress theme created by my good friends over at outerspiceweb.com. It's called Continuum, and it is spectacular. It is their newest offering.
I'm doing some work with it, so I installed it here to help me figure out how it works and debug and develop on it, but I like it so much I think I'll just leave it up. You too can have this rad-tacular theme by heading over to its page on themeforest.
- Oh, and one final thing. I am actually working on upgrading status-bar calculator for Firefox 4.0. I'm having trouble, I have no idea why it's not working, but when I figure it out there will be a write-up here.
jQuery UI Patch Landed!
I'm now an official contributer to the jQuery codebase (well, jQuery UI).
This is my first real code contribution to a major open source project. Kind of exciting.
Granted it was only one line of code (started as around 15 lines, but we got it narrowed down to just the one) but it was a big problem for my website (and several other sites) and it took some detective work to track down what the actual issue was.
Flurl – Part 1.a: Rolling your own JavaScript library, setting up the core
Flurl is a mashup I did recently as a practice exercise.
It takes a flickr panda photo-stream, displays a photo, and uses qurl to make a shortened URL link to the photo.
These are the notes I took while I was doing it.
The project can be found on GitHub at http://github.com/cmcculloh/Flurl
For this exercise I didn't want to use any JavaScript library. Normally I'd use jQuery (naturally) but I wanted to feel the pain of plain jane JavaScript again since it had been well over a year since I had done any AJAX without a library.
I decided I'd roll my own library that I could use to encapsulate the AJAX and DOM selection framework to keep it seperate from the actual app and to simplify my life in actually writing the app.
Since I wanted my library to feel a little jQuerish I decided as an homage I'd name it cQuery and use the _ instead of the $.
Step 1, the ubiquitous self executing anonymous function:
(function(window, document, undefined){})(this, document);
I'll break it down. The starting paren "(" and it's mate are just a "cool guy" coding convention to let people know, "this is weird! This is a library! This ain't yo mama's JavaScript!". It's the same as this:
function(window, document, undefined){}(this, document);
Which is simply just a function that immediately calls itself. The main reason to do this is to prepare our code for minification. When we minify, it will end up something like this:
(function(A,B,C){})(this, document);
So anywhere in our library where we had "window" or "document" or "undefined" it will not be the much shorter "A", "B" or "C", much smaller!
Paul Irish explains this in a *little* more detail in his 10 Things I Learned From the jQuery Source video.
Next we build the core function of our library, add it to the namespace and give it it's "_" shortcut:
var cQuery = function(elm){
};
window.cQuery = window._ = cQuery;
Note that if we didn't do that last line 'cQuery' would not be available in the rest of our JavaScript since it is hidden away inside of the closure we talked about above.
I really like the way jQuery works, and I want my library to mimic this. So calling:
cQuery("#domElementById").someMethod().anotherMethod();
ought to work.
Functions in JavaScript are just Objects that you can invoke. Functions can have their own methods, properties, etc. So basically cQuery is just an object that can DO something on it's own so we can say cQuery() instead of cQuery.doThing(). Much more convenient. So basically our var cQuery = function(elm){} code is just setting up my cQuery library object in a way that it can be called and passed the dom element we are working with.
Since I want to be able to "chain" things in my library, I'll need to add the methods in there that enable my chaining. I do this by ending my cQuery function with a return statement that returns an object containing the methods I wish to be available for chaining, each of these methods in turn returning an instance of the cQuery object (unless the method is specifically supposed to return something else, which makes it a destructive method because it ends my chaining), like in this example:
(function(){
var c = function(){
return{
blah:function(){
alert("blah");
return c();
},
blah2:function(){
alert("blah2");
return c();
}
};
};
window.c = c;
})();
c().blah().blah2();
That's it! Now I've got the core of my JavaScript library all set up, chaining enabled, library closed in but available on the namespace, all ready to be made useful! Here's the code we've got so far:
(function(window, document, undefined) {
var cQuery = function(elm){
//DOM selection and storage will go here
return {
//chain-able library methods go here
};
};
//make sure our library is exposed to the global namespace and make a shortcut "_" so we don't have to type cQuery every time.
window.cQuery = window._ = cQuery;
})(this, document);
You Dream You’re A Unicorn!
Sometimes you're just minding your own business writing code, reading specs, maying layouts, watching the yayQuery podcast... and sometimes you're sleeping. Sometimes you're dreaming. Apparently, sometimes, on very rare occasions you're dreaming you're a Unicorn!
The Gource of jQuery from yayQuery on Vimeo.
The visualization is actually quite cool (well, minus all the unicorn stuff). It's the jQuery project programmer source code commits over time.
It's like some developer smoked some crack and decided he was a magical wizard casting spells through his computer and these spells produced things called "programs" or "web pages". This programmer wanted to share his trip with the world and created something called "Gource". This will look at a project's source control history and create a visualization based on that. It's pretty amazing actually.
This video comes to us courtesy of the ever-amazing yayQuery podcast and Jonathan Neal, who is hilarious. Here's the mp3.
Using jQuery to bind a function to a select box change and retrieve the selected value

If you need to bind a function to be called when a user selects an option from a select box using jQuery, you've come to the right place.
There are several different ways to skin this cat, but basically here is what we are going to do:
1. Bind a change event listener to the select box itself
2. When the box is changed, call a function that detects and retrieves the selected value
Here's the code:
$("#selectBoxId").change(function(){
var selectedValue = $(this).find(":selected").val();
console.log("the value you selected: " + selectedValue);
});
Let's break it down line by line.
Line 1:
$("#selectBoxId").change(function(){
$("#selectBoxId") grabs the DOM element out of the html and makes a jQuery object.
.change(function(){}); binds a function to the change event of the select box. Any time anyone changes the selection in the select box this function will fire off
Line 2:
var selectedValue = $(this).find(":selected").val();
var selectedValue creates a new variable that the selected value will be stored in.
$(this) creates a jQuery object based on the select box that triggered the function.
.find(":selected") looks a the select box that triggered the function and finds the option that got selected.
.val() gets the "value" of the selected option.
Line 3:
console.log("the value you selected: " + selectedValue);
this just calls the firebug console in firefox and tells it you want to print something out to it. You don't need this line, but this line shows you that the above code did indeed grab the selected value out of the select box.
Line 4:
});
This just closes the open change function started on line 1.
This code assumes that you have an HTML select box with an id of "selectBoxId". If you have a series of select boxes that all need the same function bound to them for some reason you can give them all the same class name (say "selectBoxesClass") and select them like so: $(".selectBoxesClass").
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Conforming XHTML 1.0 Strict Template</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script type="text/javascript">
$(function(){
$("#selectBoxId").change(function(){
var selectedValue = $(this).find(":selected").val();
console.log("the value you selected: " + selectedValue);
});
});
</script>
</head>
<body>
<select id="selectBoxId">
<option>Foo</option>
<option>Bar</option>
<option selected="selected">Beh</option>
</select>
</body>
</html>
Here are some resources for further reading:
jQuery
jQuery change
jQuery find
jQuery val
jQuery :selected selector
jQuery Element selector
jQuery id selector
jQuery class selector
Firefox
Firebug
Firebug Console
Removing and re-using hard-coded onclick JavaScript methods with jQuery
Let's say you've been tasked with the ominous task of taking a currently "working" checkout process written in a hodge-podge of Prototype, jQuery and plain jane JavaScript and adding click methods to all input and a elements. Now, let's also say you can't modify the current HTML/JavaScript at all (mostly because it's all working and you don't want to break it and it's already indecipherable code). Now, let's also say that the click method you are attaching needs to be hands down, no matter what, the FIRST method that executes when you click on the element.
Sure you can make your JavaScript the first JavaScript that gets run on the page (so that the dynamic bindings run first, and therefor (hopefully) execute first). But what about the HARD CODED html click methods? This little code snippet will go through and dynamically remove (and "cache" (save for later)) all of the hard-coded onclick functions, and then bind your live functions to do your new critical must trigger first event, and then call the original onclick method on the element.
Yes, this code is scary. Yes, I cried a little after I wrote it while curled up in a ball under my desk. Yes, I plan to submit it to thedailywtf.com. But sometimes you just have to do what you have to do. If you're in a similar situation, this might just help you out a little. Prepare to throw up in your mouth, hold on to your hats, and dive in:
To understand this a little more, you might like to throw a whole bunch of console.log() statements in there to see the state of each variable at each step along the way. It's actually kind of fascinating that this code even works.
The toString() on the onClick on the attr call actually returns:
"function onclick(e){doThing();}"
This is scary/cool/weird since the attribute it's pulling is just
<input type="checkbox" onclick="doThing()" />
So I go through with the regex and strip off the " onclick" leaving just an anonymous function: "function(e){doThing();}"
I then assign the anonymous function to the variable "clickMethod" thus creating a brand new function on the page called clickMethod, which can be triggered or called anywhere I want and even passed an event object!!!
*BARF* XP
Someone please tell me there's a better way... If so, why not hook me up on FIXEE???
Diagonal Accordion with jQuery
diagonalAccordion is a jQuery plugin that allows for accordion functionality, but in a diagonal angle of your choice!
I forked Charles Marshall's diagonal-accordion- plugin (on github) which allowed for a 45 degree angle accordion and hacked it to allow for any angle.
Use it like:
$("#someElement").diagonalaccordion({
acc_width:500,
acc_height:300,
bar_size:45,
speed:'slow',
accordion:'.accordion',
coverage:4,
diagAdjust: 3
});
Or even just:
$("#someElement").diagonalaccordion({
diagAdjust: 5
});
The only difference between using my plugin and his is the diagAdjust parameter. This parameter allows for incremental adjustments away from 45 degree angles. The bigger number you provide, the shallower the angle gets, until at number 11 it is effectively no angle. then after 11 it starts angling the other way.
Here's the official demo for the original plugin. Like I said, mine works exactly the same way, it just has an extra param to use.
Exploring jQuery getScript, or “How I created the jQuery getScriptLite plugin”!
I set out to discover how the getScript function in jQuery works today. Here is what I found. This post sort of illustrates just how easy it is to dig into the jQuery source and really learn the library.
The first thing I did is hop over to github and pop open the source for the 1.3.2 release:
http://github.com/jquery/jquery/tree/1.3.2/src
Since getScript() is an AJAX function, I naturally clicked on "ajax.js". On line 109 I found:
Oh wow! It's just a convenience method! It doesn't do anything magical at ALL. It simply calls $.get().
Now, I could have stopped there, but for fun, let's go deeper. On line 93 I found the get() function:
Ok, so get() is really just a convenience method for ajax(), so let's look for that. Line 25, the ajax() function:
Wait, that's not the one! I don't really know how this works, but that can't be the one because it strips out the script tags. I find another one later on on line 170 that goes all the way to line 451. Way down on line 253 it seems to handle the actual script loading:
Aha! Clever. Creates a script tag and sets it's source to the one we specified and then on line 278 appends it to the head of the document, I'm assuming at that point running the script because two lines later it returns out of the function:
So, there you have it! It would almost seem that you could do the same thing by:
In fact, that seems to work just fine. Hrm... Fork me on github and/or get the plugin!






