Dav Glass just gave me a crash course in Node.js. He blew my mind. More to come.★
Posts Tagged ‘javascript’
Node.js
Tuesday, March 30th, 2010Stoyan’s Performance Advent Calendar And Me
Sunday, December 20th, 2009Stoyan Stefanov decided to release a post a day during the advent season in an effort to bolster our budding performance community.
He was kind enough to let me participate, not once but twice! My first contribution was called, JavaScript Loading Strategies and my second, which is out today is called, Extreme Performance Optimization.
I hope you enjoy reading them, and be sure to check out the other posts in Stoyan’s performance advent calendar as well! ★
Graph representations of YUI Easing Methods
Wednesday, October 28th, 2009First, for those who didn’t get the news already, I’m now working at Yahoo! as a Front-end Engineer on the Yahoo! Mail team. As a result, I’m presently down in Sunnyvale, California for a week of orientation and training. In a quirk of perfect timing, my visit coincides with the first ever YUICONF, the first day of which just ended.
The last event of the day was a “show and tell” where people showed what they’d built using YUI. Philip Tellis showed off his YUI port of the Flot graphing library which got me thinking of a little utility I’d written a while back. When I was writing the animation chapter of the YUI section of Professional JavaScript Frameworks, I needed a way to accurately display what the animation easing methods looked like. So I wrote a little utility that basically drew a chart and passed some linear data through a selected YUI easing method, then plotted the points. Voila! Instant accurate representation!★
Introducing 140 char (or less) JavaScript programs
Friday, July 18th, 2008Today I got tired of seeing yet another onclick="obtrusivejavascript()" in HTML so I wrote a little program in protest. What I wanted to do though was to post the program to Twitter which has a 140 character limit so it was mildly challenging. Here it is:
Fully expanded:
var elems = document.getElementsByTagName("*");
for (var i = 0; elems[i]; i += 1) {
if (elems[i].getAttribute("onclick")) {
elems[i].onclick = function () {
alert("FAIL!");
}
}
}
Compressed for Twitter:
var x=document.getElementsByTagName("*");for (var i=0;x[i];i++){if (x[i].getAttribute("onclick")){x[i].onclick=function(){alert("FAIL!");}}}
I invite you to continue the trend. Write 140 character (or less) JavaScript programs and post them to Twitter then post a link here in the comments. If the trend grows, I’ll build a small site to host the activity.
I’ll go first!★
A Nuance of Preventing Default
Friday, May 16th, 2008One of the most common operations when assigning event handlers is to prevent the default action the event normally triggers.
In the case of an anchor for example, the click event of an anchor triggers the default behaviour of following the URI specified in the href attribute. So the browser’s default action when clicking the “Fluffy Bunnies” anchor below, is to send the user to http://wait-till-i.com.
<a href="http://wait-till-i.com">Fluffy Bunnies</a>
When coding unobtrusive JavaScript awesomeness however, following the URI isn’t always the desired action that we want when the user clicks on an anchor. Thus, the event handler that is assigned to the anchor needs to stop the default behaviour. This can be done in two ways: by having the handler return false, or using the event object’s preventDefault method (except for in IE which makes you set returnValue to false within the event object).
Returning False
var a = document.getElementsByTagName("a")[0];
a.onclick = function () {
return false;
};
Preventing Default
var a = document.getElementsByTagName("a")[0];
a.onclick = function (e) {
e = e || event;
if (e.preventDefault) {
e.preventDefault(); // All browsers except IE
} else {
e.returnValue = false; // IE
}
};
Having to use an if/else block every time you want to use preventDefault/returnValue is very cumbersome. This is where a good JavaScript library will serve you well by wrapping this functionality up in one easy function call. The YUI Library for example allows you to do this:
var a = document.getElementsByTagName("a")[0];
a.onclick = function (e) {
e = e || event;
YAHOO.util.Event.preventDefault(e);
};
The nuance: using preventDefault in debug vs. release code
In my opinion, preventDefault should be used in two different ways depending on whether you’re in debug mode or release mode. The reason for this is simple, when you’re debugging, the last thing you want is for the browser to leave your page when something breaks on clicking an anchor. Why? Because normally (in Firebug anyway) the error message that was generated disappears once you leave the page. Take the following code example:
var a = document.getElementsByTagName("a")[0];
a.onclick = function (e) {
e = e || event;
// Lots of buggy code here
YAHOO.util.Event.preventDefault(e);
};
In this example, the code will break before preventDefault has a chance to execute. Upon its breaking, the browser will fall back to the anchor’s default behaviour and leave the page to follow the anchor’s URI. That can be annoying when you’re debugging, but it’s exactly what you want in a production environment. You don’t want links to stop working in production when some JavaScript breaks. Rather, you want the script to degrade/break gracefully and give the user access to a valid URI. So the above example is fine for production. While debugging however, the following sequence is preferable:
var a = document.getElementsByTagName("a")[0];
a.onclick = function (e) {
e = e || event;
YAHOO.util.Event.preventDefault(e); // Stop the default action before buggy code breaks
// Lots of buggy code here
};
I hope you’ve found this nuance useful, I know it’s saved me lots of headaches when debugging.★