Closed Thread Icon

Preserved Topic: Arrays Pages that link to <a href="https://ozoneasylum.com/backlink?for=18132" title="Pages that link to Preserved Topic: Arrays" rel="nofollow" >Preserved Topic: Arrays\

 
Author Thread
Wakkos
Maniac (V) Mad Scientist

From: Azylum's Secret Lab
Insane since: Oct 2000

posted posted 08-27-2001 00:55

Hi, i have this code to make that some random image change everytime that the pag is reloaded (Thanks Bitdamage, DL and mr.Max!) but now, i want a different URL and a different "alt" text foreach one, i know that i have to do a matrix (or an Array?) but i'm still lost in the topic...

Can you guys help me again?

Code:
-----------------

<script language="JavaScript">
<!--
function RandomNumber(upper_limit)
{
return Math.round(upper_limit * Math.random());
}
//-->
</script>


------------------

<script language="JavaScript">
<!--
var upper_limit = 1;
document.write('<IMG SRC="images/logos/logo' + RandomNumber(upper_limit) + '.gif" WIDTH="309" HEIGHT="95">');
//-->
</script>



.-rotate script by Mr.Max

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 08-27-2001 01:16

Although you can use objects to simplify things, it's easier like this:

wakkosArray = new Array();
wakkosArray[0] = "<IMG SRC=\"images/logos/logo1.gif\" WIDTH=\"309\" HEIGHT=\"95\">";
wakkosArray[1] = "<IMG SRC=\"images/logos/logo2.gif\" WIDTH=\"309\" HEIGHT=\"95\">";
wakkosArray[2] = "<IMG SRC=\"images/logos/logo3.gif\" WIDTH=\"309\" HEIGHT=\"95\">";

document.write(wakkosArray[RandomNumber(wakkosArray.length-1)]);

Wakkos
Maniac (V) Mad Scientist

From: Azylum's Secret Lab
Insane since: Oct 2000

posted posted 08-27-2001 01:45

Hey! i've never seen an array with my name!
Let me play with this, tell you what's happening in few minutes....


.-rotate script by Mr.Max

Wakkos
Maniac (V) Mad Scientist

From: Azylum's Secret Lab
Insane since: Oct 2000

posted posted 08-27-2001 02:10

Just E-X-E-L-E-N-T!!!

Thank you!

BTW can you email me again the rotate script, i lost my server password, and no acces to it!!
i'll appreciate that...


.-rotate script by Mr.Max

[This message has been edited by Wakkos (edited 08-27-2001).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 08-27-2001 03:05

An important note:

You're using Math.round in your random number function. You should switch that to Math.floor, and here's why.

Say i inputted 2 for the upper_limit. upper_limit*Math.random() would then be a number between zero and two. Rounding that would round all the numbers from 0 to .5 down to zero. .5 to 1.5 would round to 1, and 1.5 to 2 would round to two. The problem here is that 1 has twice the chances of being chosen that 0 and 2 have! Because zero can be rounded down to, and 2 can be rounded up to, but 1 can be rounded to from both directions.

Using Math.floor instead will round 0 to 1 down to zero, and 1 to 2 down to 1. This way, each number has an equal chance of being chosen. The only problem here is that the upper_limit is *never* chosen. So you must simply change upper_limit*Math.random() to (upper_limit+1)*Math.random().

Wakkos
Maniac (V) Mad Scientist

From: Azylum's Secret Lab
Insane since: Oct 2000

posted posted 08-27-2001 03:13

Welcome to the real world....

Wao.


i'll do that right now......

Here's what i did:


<script language="JavaScript">
<!--
wakkosArray = new Array();
wakkosArray[0] = "<A HREF=\"www.sitio.com\"><IMG SRC=\"images/logos/logo0.gif\" WIDTH=\"309\" HEIGHT=\"95\" alt=\"Hola0\" border=\"0\">";
wakkosArray[1] = "<A HREF=\"www.sitio.com\"><IMG SRC=\"images/logos/logo1.gif\" WIDTH=\"309\" HEIGHT=\"95\" alt=\"Hola1\" border=\"0\">";
wakkosArray[2] = "<A HREF=\"www.sitio.com\"><IMG SRC=\"images/logos/logo2.gif\" WIDTH=\"309\" HEIGHT=\"95\" alt=\"Hola2\" border=\"0\">";

function RandomNumber(upper_limit)
{
return Math.floor((upper_limit+1) * Math.random());
}
//-->
</script>

----------------------------


<script language="JavaScript">
<!--
var upper_limit = 1;
//document.write('<IMG SRC="images/logos/logo' + RandomNumber(upper_limit) + '.gif" WIDTH="309" HEIGHT="95">');
document.write(wakkosArray[RandomNumber(wakkosArray.length-1)]);
//-->
</script>




[This message has been edited by Wakkos (edited 08-27-2001).]

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 08-27-2001 08:17

Wakkos, you didn't add the closing </A> tag to the array elements. And you don't need that "var upper_limit = 1" definition anymore...

BTW Here's image rotator CGI script: http://www.max.co.yu/ozone/sig.pl.txt

Enjoy!



[This message has been edited by mr.maX (edited 08-27-2001).]

Wakkos
Maniac (V) Mad Scientist

From: Azylum's Secret Lab
Insane since: Oct 2000

posted posted 08-27-2001 15:15

Thank you mr.Max!

I'll delete that stupid declaration, and in my web is the </a>, dunno what happened here... a copy-past error.

And thanks again for thata amazing rotate script!


.-rotate script by Mr.Max

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 08-29-2001 11:45

mr.Max I checked your rotate script.pl.

Why are you pre-setting a header and then reading the file?

Why not not output any header at all, just pick a random image from the array and redirect to it. ? as if in PHP:

code:
<?
function getfromrandomimageslist()
{
//code here....
}
$imagepath = getfromrandomimageslist();
header("Location: $imagepath");
exit();
?>



my question is just: is there is any reason you chose to read the file instead of relocating to it?

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 08-29-2001 18:11

Since script has to be called directly from <IMG> tag, it is best not to do any redirections. Some web browsers (i.e. NN4) won't understand it correctly and either won't display anything or will always display the same image (when you click on reload). So, at the end it's best to trick the browser to think that it is actually image and not the script.

« BackwardsOnwards »

Show Forum Drop Down Menu