Beautiful Code
Beautiful Code asks well-known (and not so well-known) programmers to discuss the most beautiful piece of code they know.
From the preface:
For some, it lives in the small details of elegantly crafted software. Others find beauty in the big picture — in how a program’s structure allows it to evolve gracefully over time, or in the techniques used to build it.
Reading the first few chapters of the shiny new copy of Beautiful Code in the office has me thinking — what’s the most beautiful piece of code I’ve ever written? I took a little digging, but I think this bit of JavaScript code to preload images would be high on my personal list:
function preload(images) { var img = document.createElement('img'); img.onload = function() { this.src = images.shift() || null; } img.onload(); }
Which would be called like so:
window.onload = function() { preload(['1.png', '2.png', '3.png']); }
I think it’s beautiful because:
- It’s short. Every byte counts when the client has to download the uncompressed source code.
- It’s standards-compliant and will work in any browser.
- It’s efficient. Only one image element is created and closure is used to keep the list of images.
- It’s clever. It advances to the next image by changing the image’s
srcproperty in theonload()event handler. When that image loads,onload()changessrcagain. Rinse and repeat until there are no more images left andsrcis set to null, which breaks the loop. - When called from within
window.onload(), preloading only starts after the rest of the page has finished loading. - It’s self-contained and easy to re-use.
So that’s mine… What’s your most beautiful piece of code?
January 26th, 2009 at 12:37 pm
I’ve chosen a piece of code that’s smaller in size, however not as small as JavaScript image preloading.
Mine has to do with images also, but deals with PNG images on web sites.
Internet Explorer (older versions, at least) are not setup inherently to handle alpha transparency in PNG images, but there is scripting to allow it to. In order to have proper placeholders and such I’ve written some PHP code that I’ve used on a few web sites to handle this gracefully.
The $root variable is set on each page loaded, I always do this to work well with relative links rather than absolute, and can write the same code across files as long as $root is set properly.
There’s likely more I could add to this function to support additional IMG tag parameters, but this is pretty much all I use most of the time, so I kept it simpler.
Usage examples:
And so on…
Here’s the code:
Hope this helps someone out there, and hope you can improve on what I’ve done also. One likely improvement I will make is to utilize only one style tag, since IE will have two set in the same image since I’m closing one and opening another. It does however, work for IE prior to version 7 back to 5.5. Version 5.0 and prior are still not going to handle this gracefully, they lack the support for this filter.
There is likely some caveats to using this also, and I know there’s other ways to use it (an include JS file that is only read by IE browsers (by using an “if lt IE 7.” to load it, and using a script defer tag to render before the page). The above way works for me, and is how I’ll keep it until I’ll need additional features.
February 5th, 2009 at 4:04 pm
We prefer to let javascript handle the image transparency issue. GD can be a pain sometimes for the simple reason that not everyone has it installed and it can use a lot of resources. Great code btw, we just prefer it this way:
I was going to post a piece of javascript that I found to be beautiful, however, after reading the example in the post, I think I’ve been trumped.
February 5th, 2009 at 4:19 pm
@kevin: don’t be shy, let’s see your beautiful code!
February 5th, 2009 at 5:02 pm
lol fair enough:
Essentially this creates a toggle effect for the following xHTML
I love anything that only occupies a few lines of code but is both highly extensive as well as practical.