0

I have the following javascript code for a rotating image. Those images are in a separate folder from the javascript. I want to link the rotation itself to another page. All images will go to the same link. The current code works great but I just don't know where I add the a href code or its syntax.

// JavaScript Document
//
// Type the number of images you are rotating.
NumberOfImagesToRotate = 3;
FirstPart = '<img src="domain/rotatingimages';
LastPart = '.gif" height="250" width="388">';

function printImage() {
var r = Math.ceil(Math.random() * NumberOfImagesToRotate);
document.write(FirstPart + r + LastPart);
}
jm0
  • 3,294
  • 2
  • 16
  • 18
  • 3
    Java is not Javascript – George Nov 27 '13 at 15:57
  • Also -- I think people are trying to help you in the way you understand currently, but note that it's not really considered standard practice to construct tags out of strings like this. More common is to place the elements in HTML and then use javascript to change their attributes (see http://stackoverflow.com/questions/1232793/javascript-set-img-src) – jm0 Nov 27 '13 at 16:23

2 Answers2

3

Try this format

<a href="link_to_target" target="_blank"><img src='image_src' border="0"/></a>

It is up to you if you want to define the target attribute in <a> tag.

Uresh K
  • 1,136
  • 11
  • 16
0
// JavaScript Document
//
// Type the number of images you are rotating.
var numberOfImagesToRotate = 3;
// Edit the destination URl here
var linkUrl = 'example.html';
// HTML-partials
var imgFirstPart = '<img src="domain/rotatingimages';
var imgLastPart = '.gif" height="250" width="388" border="0">';
var anchorFirstPart = '<a href="';
var anchorSecondPart = '">';
var anchorLastPart = '</a>';

function printImage() {
  var r = Math.ceil(Math.random() * numberOfImagesToRotate);
  document.write(anchorFirstPart + linkUrl + anchorSecondPart + imgFirstPart + r + imgLastPart + anchorLastPart);
}
masswerk
  • 261
  • 1
  • 4