homepage Welcome to WebmasterWorld Guest from 54.166.10.100
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / WebmasterWorld / New To Web Development
Forum Library, Charter, Moderators: brotherhood of lan & mack

New To Web Development Forum

    
How to have images on javascript that's not random
granddeluxe




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

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]

 

rocknbil




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

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

granddeluxe




msg:4066720
 8:44 am on Jan 24, 2010 (gmt 0)

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?

rocknbil




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

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. :-)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / WebmasterWorld / New To Web Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved