R
R
Roman2014-11-29 13:48:30
JavaScript
Roman, 2014-11-29 13:48:30

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>

This script displays images randomly (but you already know this).
How to attach a specific link to a specific image?)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Max, 2014-11-29
@borNfree

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://.......'

R
Rsa97, 2014-11-29
@Rsa97

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 question

Ask a Question

731 491 924 answers to any question