Click to See Complete Forum and Search --> : page specific picture
drasque
10-16-2003, 10:41 AM
I've created a header file and a random image generator so that when you travel through my site, the pictures change randomly. I include this header file in my other pages, but now I want the pictures to be page specific. Is there a way to Compare URL"s and then get a picture from an array depending on what page is visited? I'd rather just change the header file than to put all the code into all the different pages seperately.
Thanks!
I'm new to JS, but I'm learning :)
Assuming that your header file is a .js file, and not a server-side file, you'd use something like this...
var imgName = "name_of_the_image_you_want_to_change";
var pages = ["array","of","pages"];
var imgs = ["array.gif","of.jpg","images.gif"];
for(i=0; i<pages.length; i++){
if(location.href.split("/")[1].split(".htm")[0] == pages[i])
{document.images[imgName].src=imgs[i];}
}
[J]ona
Charles
10-16-2003, 02:15 PM
This looks like a good place for an associative array.
img = new Object();
img['title of page 1'] = 'image1.png';
img['title of page 2'] = 'image2.png';
onload = function () {document.images.imageName.src = img[document.title]}
And don't forget to give your image a real "src" for those who do not use JavaScript.