I am trying to randomly display an image file on a web page. The web page is HTML but the random number generator is Javascript. The files are named 1.png, 2.png, 3.png, etc. I found some code that is supposed to generate a random number and I know know to display an image by enclosing the path within quotes (see below). But I don't know how to get the image tag to recognize the path variable generated by Javascript. Here is the code:
<td valign="top" width="470" height="203">
<script type="text/javascript">
var randNum = Math.ceil(Math.random()*3);
</script>
<p align=center><img border="0" src="images/ads/1.png">
<br>Click for enlarged image </p>
</td>
I'm fairly new to HTML and am new to Javascript. If the variable were an ASP variable, I think I could do what I need to do. I want to make the src point to the randomly selected file number, something like this src="images/ads/" & randNum & ".png"
I don't know how to use the javascript variable. Perhaps there is another way by adding something to the javascript like
var myPath = ('/images/Ads/'+randomNumber+'.png');
then if src=myPath would work, I'd have a solution. However, I can't seem to get from the javascript variable to HTML. I've spent several hours trying to find a solution on the web, to no avail. I would be grateful for any help you can give.
Also, if I could get the random number from HTML itself, that would be fine, too. I'm not tied to Javascript.
Thanks!
<td valign="top" width="470" height="203">
<script type="text/javascript">
var randNum = Math.ceil(Math.random()*3);
</script>
<p align=center><img border="0" src="images/ads/1.png">
<br>Click for enlarged image </p>
</td>
I'm fairly new to HTML and am new to Javascript. If the variable were an ASP variable, I think I could do what I need to do. I want to make the src point to the randomly selected file number, something like this src="images/ads/" & randNum & ".png"
I don't know how to use the javascript variable. Perhaps there is another way by adding something to the javascript like
var myPath = ('/images/Ads/'+randomNumber+'.png');
then if src=myPath would work, I'd have a solution. However, I can't seem to get from the javascript variable to HTML. I've spent several hours trying to find a solution on the web, to no avail. I would be grateful for any help you can give.
Also, if I could get the random number from HTML itself, that would be fine, too. I'm not tied to Javascript.
Thanks!