QUOTE(Arbitrator @ Posted 1 Jun, 2007 - 09:33 AM)
After reading it, you should be able to tell that your first and fourth use of the Math.random() method only generates integers between 2 and 3 inclusive; the second only generates integers between 13 and 25 inclusive; and the third between 12 and 23 inclusive. This is clearly not what you wanted, since some of the values that you’re trying to match fall out of those ranges.
I realize that I forgot the order of operations, so this information is incorrect. I believe that your use of
Math.random() is correct.
QUOTE(Squirrel @ 2 Jun, 2007 - 03:30 PM)

I understand actually what they are supposed to do, but I still cant get this line of code to work in IE. The image tag in question is named "randomprogram" so it should adjust it accordingly but it wont. Any ideas?
- randomprogram is not a valid element name in HTML 4.01. If you want to add an image, you use the img or object elements.
- Once you create the element using createElement(), you need to add it to the document by using something like appendChild() or insertBefore().
- If you’re altering the src attribute of an existing image, you wouldn’t use createElement() at all. Instead, you would target the element through the use of getElementById(), getElementsByTagName(), or some other method. If you’re trying to target it by reference to a name attribute, you shouldn’t be; those are obsolete (in favor of IDs) except on form control elements.
An example revision of your last code snippet as it applies to an existing image:
CODE
<img id="content" alt="…" width="…" height="…" src="…">
CODE
var image = document.getElementById("randomprogram");
var rndprog = Math.floor((2 * Math.random()) + 1);
switch(rndprog) {
case 1:
image.setAttribute("src", "images/thumb_insultgenerator.jpg");
break;
case 2:
image.setAttribute("src", "images/thumb_income.jpg");
break;
}
If the image is meaningless without JavaScript, then I would create and insert it entirely using JavaScript so that it’s not seen when the user has JavaScript disabled. For example, if the
alt,
width,
height, or
src attributes are initially missing from the source (or left blank) because they need to be filled in via script, then this would apply. Example:
CODE
<div id="gallery"></div>
CODE
var image = document.createElement("img");
var rndprog = Math.floor((2 * Math.random()) + 1);
switch(rndprog) {
case 1:
image.setAttribute("alt", "<description>");
image.setAttribute("width", "<width>");
image.setAttribute("height", "<height>");
image.setAttribute("src", "images/thumb_insultgenerator.jpg");
break;
case 2:
image.setAttribute("alt", "<description>");
image.setAttribute("width", "<width>");
image.setAttribute("height", "<height>");
image.setAttribute("src", "images/thumb_income.jpg");
break;
}
document.getElementById("gallery").appendChild(image);
Note that the
width and
height attributes are recommended, but optional. The
alt attribute is required in HTML 4.01.
This post has been edited by Arbitrator: 2 Jun, 2007 - 10:07 PM