We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Javascript; Quick bugshoot please
Topic Started: Apr 17 2005, 12:45 PM (392 Views)
JoeC
Euch! IE tastes horrible!
[ *  *  *  *  * ]
Code:
 
<script language="Javascript">
function fishy() {
var core = 0;

function initArray() {

this.length = initArray.arguments.length;
 for (var i = 0; i < this.length; i++) {
 this[i] = initArray.arguments[i];
 }
}

link = new initArray(
"http://www.site1.com",
"http://www.site2.co.uk",
"http://www.site3.org"
);

image = new initArray(
"1.jpg",
"2.jpg",
"3.jpg"
);

text = new initArray(
"Site One Alt Text",
"Site Two Alt Text",
"Site Three Alt Text"
);

var core = (Math.round((Math.random()*2)+1));
var ranlink  = link[core];
var ranimage = image[core];
var rantext  = text[core];

document.write('<a href=\"' +ranlink+ '\"><img src=\"'+ranimage+'\" border="0"

alt=\"'+rantext+'\"></a>');
}
//-->
</SCRIPT>


OK, that's my script, in the Head of my HTML page. I then call it later, four times, with

Code:
 
<script type="text/javascript">
 <!--
 fishy( );
 // -->
 </script>


But, some of the links show fine, while others show up as "undefined". I think I'm missing something stupidly obvious. Any help please?
Also, sometimes the Alt text shows, while other times, the "user posted image" comes up. Help?
Offline Profile Quote Post Goto Top
 
EvilSteve
Member
[ * ]

Hey man, give this a try:

Code:
 

<script language="Javascript">

var link = new Array(
"http://www.site1.com",
"http://www.site2.co.uk",
"http://www.site3.org"
);

var image = new Array(
"1.jpg",
"2.jpg",
"3.jpg"
);

var text = new Array(
"Site One Alt Text",
"Site Two Alt Text",
"Site Three Alt Text"
);

function fishy() {

var core = Math.floor(Math.random() * link.length);

var ranlink  = link[core];
var ranimage = image[core];
var rantext  = text[core];

document.write('<a href=\"' +ranlink+ '\"><img src=\"'+ranimage+'\" border="0" alt=\"'+rantext+'\"></a>');

}
</script>


I think your main problem was in the line that creates the random number. If you use round, sometimes it rounds up and sometimes it rounds down, and if it was getting rounded up, the index you were using for your arrays was going out of bounds and giving you "undefined"s. I also switched the code to use regular JavaScript Arrays, and moved the array creation outside of the function code so that the arrays only need to be created once instead of everytime the function is called.
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Technology Chat · Next Topic »
Add Reply