-1

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);
}
2
  • 3
    Java is not Javascript Commented Nov 27, 2013 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 stackoverflow.com/questions/1232793/javascript-set-img-src) Commented Nov 27, 2013 at 16:23

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.