Busted Mug

A blog that documents solutions to the most frustrating problems that occur during development in technologies such as Java, XML, AJAX, SQL, CSS and others that make me want to throw my coffee mug against the cube wall.

Monday, March 19, 2007

Busted Mug: Dynamic Assignment of Event Handlers - A generic rollover event

Busted Mug: Dynamic Assignment of Event Handlers - A generic rollover event - Gosh, what was I thinking? You've gotta preload those rollovers! Here's a revised version using a dynamically created function as the dynamically created event handler. Say that ten times fast...you'll probably want to dynamically go create yourself a cup of coffee ;)
//bch: better code for rollovers
function start(){
    var els = document.getElementsByTagName('img');
    for (i=0; i<els.length; i++){
        if(els[i].className 
== "rollover"){ 
            var tmp 
= new Image();
            
            //save out source
            tmp.src 
= els[i].src;
            outs[i] 
= tmp;
            
            //save over source
            tmp.src 
= els[i].substring(0,src.length-4) + "_over" + src.substring(src.length-4);
            overs[i] 
= tmp;
        
            //set event handlers w/ dynamic functions
            els[i].onmouseover 
= eval(new function (){ this.src = overs[i].src});
            els[i].onmouseout 
= eval(new function (){ this.src = outs[i].src});
        }
    }
}

0 Comments:

Post a Comment

<< Home