Wednesday Afternoon Open Thread
Here’s an open thread for a hump day afternoon.
And since tech threads always turn into open threads anyway, here’s another bit of info that might be interesting to Javascript geeks. I mentioned that I’ve been working with the jQuery Javascript library to do the ‘slide down’ special effects in our commenting engine; but it does a lot more than fancy eye candy. Here’s an example showing how much time and code the library can save.
If you’re not logged in, the little buttons that activate the ‘reply’ and ‘quote’ features on the top line of each comment are hidden. So if you’re on a page containing comments and you log in, the commenting engine needs to go through all the existing comments, and make the ‘reply’ and ‘quote’ links visible.
Before jQuery, this was the code that showed those links; to find them, each set of reply/quote links is wrapped in a tag that has the CSS class ‘replylink.’ First, since there’s no native Javascript function to find all elements with a certain class, we need a special function:
var classElements = new Array();
if ( node == null )
node = document;
if ( tag == null )
tag = ‘*’;
var els = node.getElementsByTagName(tag);
var elsLen = els.length;
var pattern = new RegExp(‘(^|\s)’+searchClass+’(\s|$)’);
for (i = 0, j = 0; i < elsLen; i++) {
if ( pattern.test(els[i].className) ) {
classElements[j] = els[i];
j++;
}
}
return classElements;
}
The code to actually show the elements calls this function, then loops through the results, setting the ‘display’ property for each one so that it becomes visible.
for (var i = 0; i < theReplylinks.length; i++) {
theReplylinks[i].style.display = ‘inline’;
}
Now here’s the cool part. Using jQuery to show all the ‘replylink’ elements on the page reduces all this code down to one—count it, one—simple line:
That ends our geek exploration of the day. The thread is now open…