Courtesy of Daniel Nolan, I found this handy little rollover JS to add rollovers for all your images. It is standards compliant and will not invalidate your HTML. It also majorly cuts down on the bulkiness of your HTML, eliminating the inline JS that is so common on sites nowadays for image rollovers.
Why not just use pure CSS?
CSS allows use of the :hover pseudo-class to swap images. This eliminates nasty inline javascript nonsense to accomplish the same thing and even works with JavaScript disabled. Why not use this method?
I asked myself the same question. There are two answers:
- Safari does not support image replacement on form elements. Thus, the solution would only work some of the time.
- IE6 only supports :hover on anchor tags. “Who cares?” you ask. Well, clients who pay money for their sites to work for the largest audience possible care. And, people who don’t upgrade their machines care. So, anyone with IE6 would lose the image rollover. Isn’t that sad? Especially when you can include them so simply?
How to use it
- Have a consistent naming convention for your image (and its rollover)
For example, name your image myimage.jpg, and your rollover myimage_o.jpg. The JS function handles the rest. It even handles the image preloads. Isn’t that nice?
- Add the class “imgover” to the image which you would like the rollover applied.
- Include the rollover.js file in your site template. If you have a whole bunch of functions executing on the window.onload, delete that line from the bottom of rollover.js and add initRollovers(); to your preload function.
I made a minor addition to include inputs of type “image”, as it was hugely cumbersome to me on a site that already had these implemented. My addition is copied below:
var aInputs = document.getElementsByTagName(’input’);
for (var i = 0; i < aInputs.length; i++){
if(aInputs[i].className == ‘imgover’){
if(aInputs[i].getAttribute(’type’) == ‘image’){
var src = aInputs[i].getAttribute(’src’);
var ftype = src.substring(src.lastIndexOf(’.'), src.length);
var hsrc = src.replace(ftype, ‘-over’+ftype);
aInputs[i].setAttribute(’hsrc’, hsrc);
aPreLoad[i] = new Image();
aPreLoad[i].src = hsrc;
aInputs[i].onmouseover = function() {
sTempSrc = this.getAttribute(’src’);
this.setAttribute(’src’, this.getAttribute(’hsrc’));
}
aInputs[i].onmouseout = function() {
if (!sTempSrc) sTempSrc = this.getAttribute(’src’).replace(’-over’+ftype, ftype);
this.setAttribute(’src’, sTempSrc);
}
}
}
}

November 5th, 2007 at 4:35 pm
Nice little script… though I usually just add some hover:anything support (http://www.xs4all.nl/~peterned/csshover.html) script in an IE conditional comment.
Also, rather than swapping images, sometimes it can be easier to combine the two images into one that is twice the height (or width) and simply adjusting the background-position property — saves having to preload.
July 18th, 2008 at 2:59 am
Hello,
I really don’t see the use of it, as long as you can do it in at least 2 ways with CSS..
1. …is the :hover you already mentioned, and frankly IE6 is soon to be replaced (as IE8 is on it’s way; also, in Safari you cannot make a form out of graphics, so it’ll show up like that anyway)
2. as Al said, use one single image for the rollover, and adjust it’s background-position. Fairly easy and with better support.
And less code, too
Anyway, I apreciate your time for sharing this with us :), thanks.