Jump to content

Javascript Malfunction


Spunky

Recommended Posts

Ok. If you go to this website, you will see three images of houses, their addresses, and links off to the right hand side of the site. You will also note that each time you visit/refresh this site the images/address change. It was hard working getting the code used to create this and I am really satisfied with it. I found another use for it to use on my website, except, I only want the images to appear randomly in 1 slot rather than 3. Following me so far? Thing is, When I modify the code accordingly it works beautifully in Mozilla and I think other browsers, but, it doesn't work at all in IE. With only making small changes it has this effect in IE for some reason. I use different links on my website but I will post the code for the linked website as well as what it takes to make it not work any longer in IE. I only shortened the amount of images so it wouldn't be so long.. Here is the code while it works in IE..

<img id="i1" src="" alt="" /><br /><a id="l1"><span id="t1"></span></a><br /><br /><img id="i2" src="" alt="" /><br /><a id="l2"><span id="t2"></span></a><br /><br /><img id="i3" src="" alt="" /><br /><a id="l3"><span id="t3"></span></a><script type="text/javascript"><!--var theImages = [{  url: "http://dawndause.com/images/crescenzo_front_fh.jpg",  title: "2617 Crescenzo Drive <br /> Joliet, IL",  link: "http://www.dawndause.com/crescenzo.htm"},{  url: "http://dawndause.com/images/ingalls_fh.jpg",  title: "118 Ingalls Ave #3 <br /> Joliet, IL",  link: "http://www.dawndause.com/ingalls.htm"},{  url: "http://dawndause.com/images/park_river_front_fh.jpg",  title: "24751 Park River Lane <br /> Shorewood, IL",  link: "http://www.dawndause.com/park_river.htm"},{  url: "http://dawndause.com/images/arboretum_front_fh.jpg",  title: "443 Arboretum Drive <br /> Lombard, IL",  link: "http://www.dawndause.com/arboretum.htm"},{  url: "http://dawndause.com/images/apollo_front_fh.jpg",  title: "417 Apollo Drive <br /> Joliet, IL",  link: "http://www.dawndause.com/apollo.htm"},{  url: "http://dawndause.com/images/keystone_front_fh.jpg",  title: "5463 Keystone Court <br /> Plainfield, IL",  link: "http://www.dawndause.com/keystone.htm"},];var randomNumber;var previouslyUsedRandomNumbers = "";for(var i = 1; i <= 3; i++){	do	{		randomNumber = Math.floor(Math.random() * theImages.length);	}	while(previouslyUsedRandomNumbers.indexOf("|" + randomNumber + "|") >= 0);	previouslyUsedRandomNumbers += "|" + randomNumber + "|";	document.getElementById("i" + i).src= theImages[randomNumber].url;	document.getElementById("t" + i).innerHTML= theImages[randomNumber].title;	document.getElementById("l" + i).href= theImages[randomNumber].link;}//--></script>

Here is the code while it doesnt work in IE..

<img id="i1" src="" alt="" /><br /><a id="l1"><span id="t1"></span></a><br /><br /><img id="i2" src="" alt="" /><br /><a id="l2"><span id="t2"></span></a><br /><br /><img id="i3" src="" alt="" /><br /><a id="l3"><span id="t3"></span></a><script type="text/javascript"><!--var theImages = [{  url: "http://dawndause.com/images/crescenzo_front_fh.jpg",},{  url: "http://dawndause.com/images/ingalls_fh.jpg",},{  url: "http://dawndause.com/images/park_river_front_fh.jpg",},{  url: "http://dawndause.com/images/arboretum_front_fh.jpg",},{  url: "http://dawndause.com/images/apollo_front_fh.jpg",},{  url: "http://dawndause.com/images/keystone_front_fh.jpg",},];var randomNumber;var previouslyUsedRandomNumbers = "";for(var i = 1; i <= 1; i++){	do	{		randomNumber = Math.floor(Math.random() * theImages.length);	}	while(previouslyUsedRandomNumbers.indexOf("|" + randomNumber + "|") >= 0);	previouslyUsedRandomNumbers += "|" + randomNumber + "|";	document.getElementById("i" + i).src= theImages[randomNumber].url;}//--></script>

The only real changes are there isn't 'title:' or 'link:' filling up "theImages", in the first code one of the lines in the javascript code shows "for(vari=1; i<=3; i++)" while in the second code it shows "for(var=1, i<=1; i++)", and the very last lines the "document.getElementById("t" + i).innerHTML= theImages[randomNumber].title; document.getElementById("l" + i).href= theImages[randomNumber].link;"are missing in the second code, since they are not needed since there is no 'title:' or 'link'.Here is my website that I am actually trying to get the code to work on. The code there is slightly different cuz others from a non-javascript-specific forum were trying to help me, dont worry about that though, it's not working for the same reason the second set of code I posted is not so just worry about that...I thank anyone in advanced for any help. :)

Link to comment
Share on other sites

You shortened the list of members in your objects from 3 to 1. But you left the comma that was used to separate the first member from the second. Firefox won't mind, but Explorer will break. It might not break things, but having a comma after the last item in an array strikes me as inconsistent, so I remove those as well:{ url: "http://dawndause.com/images/crescenzo_front_fh.jpg", <-- remove this comma in all objects}, <-- remove this comma from the last object in the arrayRemember I'm discussing the new code, not the old code.

Link to comment
Share on other sites

Omg thank you so much! That worked perfectly! I can't believe I didnt think to take out those commas since there was no need for them! Thank you so much!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...