Jump to content

Random Image trouble...


ZmanZL

Recommended Posts

I've been having a little toruble with IE displaying random pics. It seems that IE dislikes the

document.write();

tag. On another forum somebody said that I could edit an existing <img> tag using the DOM but I am not sure about how to do this, please help me.Here's the code:

function getPic() { // Create Arrays Image = new Array(4); Game = new Array(4); f=new Array(4); c=new Array(4); //Initialize arrays with Pics Image[0] ="http://lco8.atspace.com/games/Images/zz.gif" f[0] = "32" c[0]= "64" Game[0]="Land of Destiny" Image[1] = "http://lco8.atspace.com/games/Images/rya.gif" f[1] = "32" c[1] = "64" Game[1]="Land of Destiny" Image[2]="http://lco8.atspace.com/games/Images/valkal.gif" f[2] = "32" c[2] = "64" Game[2] = "Land of Destiny" //get random index into the arrays i= Math.floor(Math.random()*3); //write it out as HTML document.write('<IMG SRC="' + Image[i] + '" WIDTH="'+f[i]+'" HEIGHT="'+c[i]+'"><br>'); document.write( Game[d]); }

Link to comment
Share on other sites

But there is an easier way (at least I think it's easier) to create random images:1. You create a Math.random variable

var rdm=Math.random()
This will generate a number.2. Create a simple if...else...if that will put one image for each result of the rdm variable, so, if you want 10 possible images it will be something like that:
if (rdm < 0.1){document.write ("<img src='imageurl'>")}else if (rdm > 0.1 && rdm < 0.2){document.write ("<img src='imageurl'>")}else if (rdm > 0.2 && rdm < 0.3){document.write ("<img src='imageurl'>")}else if (rdm > 0.3 && rdm < 0.4){document.write ("<img src='imageurl'>")}else if (rdm > 0.4 && rdm < 0.5){document.write ("<img src='imageurl'>")}else if (rdm > 0.5 && rdm < 0.6){document.write ("<img src='imageurl'>")}else if (rdm > 0.6 && rdm < 0.7){document.write ("<img src='imageurl'>")}else if (rdm > 0.7 && rdm < 0.8){document.write ("<img src='imageurl'>")}else if (rdm > 0.8 && rdm < 0.9){document.write ("<img src='imageurl'>")}else if (rdm > 0.9 && rdm < 1){document.write ("<img src='imageurl'>")}
Simple, isn't it? To other number of variables just divide 1 for the numbers of images you want and keep increasing the result when defining the if condition.Sorry for any gramatical errors.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...