views:

25

answers:

1

Hi, I've made a simple slide show using Javascript, but now I want to add links to the images as they slide through, so that a pdf opens in a separate window. Can anyone tell me how to do this? Here's the script I have so far. Thanks! Julia

              var dimages=new Array();
    var numImages=3;
    for (i=0; i<numImages; i++)
    {
      dimages[i]=new Image();
      dimages[i].src="images/image"+(i+1)+".jpg";
    }
    var curImage=-1;

    function swapPicture()
    {
      if (document.images)
      {
        var nextImage=curImage+1;
        if (nextImage>=numImages)
          nextImage=0;
        if (dimages[nextImage] && dimages[nextImage].complete)
        {
          var target=0;
          if (document.images.myImage)
            target=document.images.myImage;
          if (document.all && document.getElementById("myImage"))
            target=document.getElementById("myImage");

          // make sure target is valid.  It might not be valid
          //   if the page has not finished loading
          if (target)
          {
            target.src=dimages[nextImage].src;
            curImage=nextImage;
          }

          setTimeout("swapPicture()", 5000);

        }
        else
        {
          setTimeout("swapPicture()", 500);
        }
      }
    }

    setTimeout("swapPicture()", 5000);
A: 

I think the best you can do is adding an anchor element to your HTML, like

<a id="imagelink"><img id="myImage" …></a>

and change its href (and perhaps its title) when changing the image:

// make sure target is valid.  It might not be valid
//   if the page has not finished loading
if (target)
{
  target.src=dimages[nextImage].src;
  targetlink = document.getElementById("imagelink");
  targetlink.href = add-link-here;
  targetlink.title = "title message";
  curImage=nextImage;
}

Also, you're testing for the presence of myImage twice. First here:

if (document.all && document.getElementById("myImage"))

and then here:

target=document.getElementById("myImage");
…
if (target)
Marcel Korpel