Close but no cigar:
Code:
var img = document.images['image_name_or_id'],
n = Math.random() * 100;
if(n < 70)
img.src = "1.jpg";
else if(n < 95)
img.src = "2.jpg";
else
img.src = "3.jpg";
It could perhaps be neatened:
Code:
function getFromRange(o, n) {
var last, x;
for(var x in o)
if(o.hasOwnProperty(x) && !isNaN(+x)) {
if(+x < n)
return o[last] || o.default;
last = x;
}
return o.default;
}
document.images['image_name_or_id'] = getFromRange({
'70' : '1.jpg',
'95' : '2.jpg',
'default' : '3.jpg'
}, Math.random() * 100);
Bookmarks