Answer the question
In order to leave comments, you need to log in
How to make pictures with links?
<script language="JavaScript">
<!--
var a=Math.round(Math.random()*2)
image = new Array();
image[0]="http://example/templates/skin/skinname/images/1.png"
image[1]="http://example/templates/skin/skinname/images/2.png"
image[2]="http://example/templates/skin/skinname/images/3.png"
document.write ("<img src="+image[a]+">");
//-->
</script>
Answer the question
In order to leave comments, you need to log in
Do you need to attach a specific link from the given array to a specific img tag?
If these are new img tags, then I don’t understand what the problem is, but if you want to attach some kind of link to an existing img tag, then you need to find it first in the DOM, and then change it.
Make an attribute for the img tag, for example id
Well, change the src attribute with the link you need, having previously found the tag itself on the image element on the page
document.getElementById("logo").src = 'http://.......'
Well, firstly, the frequency of the second picture will be twice as high as the first and third (0 - [0; 0.5), 1 - [0.5; 1.5), 2 - [1.5; 2)). For uniform distribution, the formula Math.floor(Math.random()*N) is used. In order for the image to be a link, it is enough to wrap it in an <a> tag. In general, it's better to write it like this:
var images = ;
var a = Math.floor(Math.random()*images.length);
document.write('<a href="'+images[a][1]+'"><img src="'+images[a][0]+'"></a>');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question