I'm just trying to figure out how to simplify this repetitive set of possible url's to replace:
Code:
var url = (document[picName].src);
// these all were to replace any of the possible choices if previously made. need to condense
url = url.replace("_transparent", choice);
url = url.replace("_auriculate", choice);
url = url.replace("_cordate", choice);
url = url.replace("_cuneate", choice);
url = url.replace("_deltoid", choice);
url = url.replace("_elliptic", choice);
url = url.replace("_ensiform", choice);
url = url.replace("_falcate", choice);
url = url.replace("_filiform", choice);
document[picName].src = url;
here is a different way of doing it by using an array of matches instead of a RegExp.
this makes it simpler to read and expand (avoiding RX's escapes) and it executes faster than RegExps as well.
Code:
var url = (document[picName].src);
function repAll( source, froms, to){
var var i=0, mx=froms.length, it;
for(i;i<mx;i++){
source=source.split(r[i]).join(to);
}
return source;
}
var keys=["_transparent", "_auriculate", "_cordate", "_cuneate",
"_deltoid", "_elliptic", "_ensiform", "_falcate", "_filiform"] ;
document[picName].src = repAll(url, keys, choice );
just another way to skin a cat in js...
__________________ my site (updated 5/13) STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%