Click to See Complete Forum and Search --> : image rotation on entry
Janna
07-15-2003, 01:47 PM
Mr. J
I've not been successful in getting the script to work. here is my code:
<script language="javascript">
<!--
mypics=new Array
("1.jpg", "2.jpg", "3.jpg")
function show_pic(){
randomNum=Math.floor(Math.random() * mypics.length);
document.getElementById("myimage").src=mypics[randomNum];
alert(randomNum)
}
//-->
</script>
<img id="myimage" src="1.jpg">
all put inside the body tags. The only picture that shows is the first one, even on refresh or re-entry.
Can you help?
Thanks, Janna
David Harrison
07-15-2003, 02:21 PM
Change this line:
mypics=new Array
to this:
var mypics=new Array();
And also you may want to change this line:
randomNum=Math.floor(Math.random() * mypics.length);
to this:
randomNum=Math.round(Math.random() * (mypics.length-1));
Janna
07-15-2003, 02:42 PM
this is what my code looks like now:
<script language="javascript">
<!--
var mypics=new Array();
("1.jpg", "2.jpg", "3.jpg");
function show_pic(){
randomNum=Math.round(Math.random() * (mypics.length-1));
document.getElementById("myimage").src=mypics[randomNum];
alert(randomNum)
}
//-->
</script>
<img id="myimage" src="1.jpg">
Still the image does not rotate on entry, it always only has the first picture up. All the pics are in the same place and I've checked their names, ha, 1,2,3...
Incase this is the problem, here is a copy of my body tag
<body bgcolor="#ffffff"ONLOAD="preloadImages();" onload="show_pic()">
I am also using JavaScript1.2, does that matter?
Thanks for the help, Janna:confused:
Charles
07-15-2003, 02:58 PM
You cannot change the "src" property of an image until it exists but you can draw it with a random "src" attribute in the first place. Put his in the document's HEAD element:
<script type="text/javascript">
<!--
Array.prototype.random = function () {return this[Math.round (Math.random() * Math.ceil (this.length / 10) * 10) % this.length]}
// -->
</script>
And this where you want the image to appear.
<script type="text/javascript">
<!--
document.write('<img src="', ['1.jpg', '2.jpg', '3.jpg'].random(), '" alt="[a random image]">');
// -->
</script>
<noscript><img src="noScript.jpg" alt="[default image]"></noscript>
Janna
07-15-2003, 03:23 PM
Thank you, that worked!!! :D
Thanks again. Janna
Janna
07-28-2003, 03:19 PM
Charles:
I'm not sure what happened, but I can not get this to work again. Can you tell me what is most likely my problem. ha, you know what I mean.
<head>
<script type="text/javascript">
<!--
Array.random = function () {return this[Math.round
(Math.random() * Math.ceil (this.length /10) * 10 %
this length]}
// --></script>
</head>
<body
<script type="text/javascript">
<!--
document.write ('<img src="' , ['1.jpg', '2.jpg', '3.jpg'].random(),
'" alt="[random Fish & Wildlife Image]">');
// --> </script>
<noscript><image src="1.jpg" alt="[default image]"></noscript>
I am putting the images on the same level as the page that I'm inserting the code into. BUT would like to put them in a folder, can you also tell me where I'd write that?
Thanks, Janna
Charles
07-28-2003, 04:15 PM
From here it looks like you've dropped the '>' in your BODY tag.
Janna
07-28-2003, 04:36 PM
Mr. J
I got the script to work, thank you. Now, I'd like to put the images in folders. When I do this, I loose them. Will you tell me where to insert my folder name? Thanks, Janna
Janna
07-28-2003, 04:39 PM
Hi Charles:
On my page, I do have the body tag inclosed. Something else is wrong, but I can not find it. Thanks, Janna
Any other help will be appreciated.
j
Charles
07-28-2003, 04:41 PM
Since your posting does not reflect what is on your page, you would do well to give me the URL of the page.
Janna
07-29-2003, 09:36 AM
Sorry Charles, I really appreciate your help. I am working on the Intranet, right now I don't have anywhere to post. We are due to shut down, I'm Federal, today at 9:00am, so I don't really have enough time to do anything else. (Like get space somewhere.) If we don't shut down and I can, I'll post and let you know. Thanks, Janna