Welcome to WebmasterWorld Guest from 54.197.116.116

Forum Moderators: brotherhood of lan & mack

Message Too Old, No Replies

How to have images on javascript that's not random

   
12:22 am on Jan 24, 2010 (gmt 0)

5+ Year Member



Hi webmasters,

I've this javascript that show random images. I use because i like it to be more organized. I used this script on my table. eg.

<script type="text/javascript" src="../scripts/java.js"></script>

the problem is i dont want my images to go random. i know that the images goes random is becasue i have the math.floor. etc..

Please can someone get rid of the images to go randomly..

thanks

here is the script:


var img_width = "150";
var img_height = "175";
var img_title = "Great Deals";

var ad=new Array()
//insert here your images src
ad[0]='http://example.com/0013.jpg';
ad[1]='http://example.com/2ecgc9g.jpg';
ad[2]='http://example.com/igcaqw.jpg';
ad[3]='http://example.com/0001.jpg';

var links=new Array()
//insert here your links
links[0]='http://example.com/ngcle4';
links[1]='http://example.com/yhwwm9y';
links[2]='http://example.com/yjxhgp4';
links[3]='http://example.com/yhfamgy';

[b]var xy=Math.floor(Math.random()*ad.length);[/b]

document.write('<a href="'+links[xy]+'" target="_blank"><img src="'+ad[xy]+'" width="'+img_width+'" height="'+img_height+'" alt="'+img_title+'" border="0"></a>');
// End -->

[edited by: mack at 9:13 am (utc) on Jan. 25, 2010]
[edit reason] example.com used for links. [/edit]

2:30 am on Jan 24, 2010 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Welcome aboard, granddeluxe! Edit your post, change your links to "example.com", see TOS [webmasterworld.com]. Note how I've commented out the code with //, meaning, if it breaks you can always set it back.

//var xy=Math.floor(Math.random()*ad.length);

//document.write('<a href="'+links[xy]+'" target="_blank"><img src="'+ad[xy]+'" width="'+img_width+'" height="'+img_height+'" alt="'+img_title+'" border="0"></a>');


for (i=0;i<ad.length;i++) {
document.write('<a href="'+links[i]+
'" target="_blank"><img src="'+ad[i]+
'" width="'+img_width+'" height="'+img_height+
'" alt="'+img_title+'" border="0"></a>');
}
8:44 am on Jan 24, 2010 (gmt 0)

5+ Year Member



Hi Rockinbil,

sorry..didn't realize i put the links there..i cant edit it back..bummer..

anyway thanks for the reply..but i dont know where to put that script you just gave me..

did u mean that i have to delete the:

var xy=Math.floor(Math.random()*ad.length);

document.write('<a href="'+links[xy]+'" target="_blank"><img src="'+ad[xy]+'" width="'+img_width+'" height="'+img_height+'" alt="'+img_title+'" border="0"></a>');
// End -->

and change it to your new script:
for (i=0;i<ad.length;i++) {
document.write('<a href="'+links[i]+
'" target="_blank"><img src="'+ad[i]+
'" width="'+img_width+'" height="'+img_height+
'" alt="'+img_title+'" border="0"></a>');
}

is this correct?

8:46 pm on Jan 24, 2010 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



In reality comment is the same thing as deleting the line. That is, in Javascript, any line that begins with this

// this is a comment

will be ignored. It's a great way to experiment with other options and if it doesn't work, you can remove the comment lines and you're back where you were.

So the entire chunk of code I pasted can REPLACE the corresponding existing lines in the code. It all goes in the bottom of the code chunk, before your "// --> End" comment.

The logic is, instead of using random to set the image and link, it iterates through the list of images, hence the "i" variable.

Put it in a test page, try it out. :-)