PDA

View Full Version : js random actions



morse
February 23rd, 2003, 05:44 AM
<SCRIPT language="Javascript">
<!--
rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
};

function rand(number) {
return Math.ceil(rnd()*number);
};

// end central randomizer. -->
</SCRIPT>
<SCRIPT language="Javascript">
<!--
picnum = rand(4);
if (picnum==1) {img="http://www.morsedesigns.com//bigk/1.gif"; url="http://www.morsedesigns.com"; }
else if (picnum==2) {img="http://www.morsedesigns.com//bigk/2.gif"; url="http://www.morsedesigns.com"; }
else if (picnum==3) {img="http://www.morsedesigns.com//bigk/3.gif"; url="http://www.morsedesigns.com"; }
else {img="http://www.morsedesigns.com//bigk/4.gif"; url="http://www.morsedesigns.com"; }

document.write('<a href="'+url+'"><img src="'+img+'"></a>');

// -->
</SCRIPT>

help ^ no work

CyanBlue
February 23rd, 2003, 05:50 AM
Curious why you don't see what's in the tag... Hm... Strange...
Originally posted by morse

<SCRIPT language="Javascript">
<!--
rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
};

function rand(number) {
return Math.ceil(rnd()*number);
};

// end central randomizer. -->
</SCRIPT>
<SCRIPT language="Javascript">
<!--
picnum = rand(4);
if (picnum==1) {img="http://www.morsedesigns.com//bigk/1.gif"; url="http://www.morsedesigns.com"; }
else if (picnum==2) {img="http://www.morsedesigns.com//bigk/2.gif"; url="http://www.morsedesigns.com"; }
else if (picnum==3) {img="http://www.morsedesigns.com//bigk/3.gif"; url="http://www.morsedesigns.com"; }
else {img="http://www.morsedesigns.com//bigk/4.gif"; url="http://www.morsedesigns.com"; }

document.write('<a href="'+url+'"><img src="'+img+'"></a>');

// -->
</SCRIPT>

<SCRIPT language="Javascript">
<!--
rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
};

function rand(number) {
return Math.ceil(rnd()*number);
};

// end central randomizer. -->
</SCRIPT>
<SCRIPT language="Javascript">
<!--
picnum = rand(4);
if (picnum==1) {img="http://www.morsedesigns.com//bigk/1.gif"; url="http://www.morsedesigns.com"; }
else if (picnum==2) {img="http://www.morsedesigns.com//bigk/2.gif"; url="http://www.morsedesigns.com"; }
else if (picnum==3) {img="http://www.morsedesigns.com//bigk/3.gif"; url="http://www.morsedesigns.com"; }
else {img="http://www.morsedesigns.com//bigk/4.gif"; url="http://www.morsedesigns.com"; }

document.write('<a href="'+url+'"><img src="'+img+'"></a>');

// -->
</SCRIPT>

morse
February 23rd, 2003, 05:52 AM
eh? when i posted with code brackets it didnt work, but i changed to php if that is what you mean.

lostinbeta
February 23rd, 2003, 01:11 PM
Why don't you store the images in array and choose randomly from that array?

morse
February 23rd, 2003, 04:12 PM
Like this? Something is wrong... I suck too much to figure out what...


<SCRIPT LANGUAGE="JAVASCRIPT">
var img_rnd = new Array ("1.gif", "2.gif", "3.gif", "4.gif", "5.gif", "6.gif", "7.gif", "8.gif", "9.gif", "10.gif", "11.gif", "12.gif");
var i = Math.round(Math.random() * 11);
document.write('<a href="http://www.morsedesigns.com" target="_new"> <IMG SRC="www.morsedesigns.com//bigk/' + img_rnd[i]>');
</SCRIPT>

CyanBlue
February 23rd, 2003, 04:49 PM
What about this??? You had non matching quotes problem... I do see the random file names with the alert() after I fixed that problem...
<SCRIPT LANGUAGE="JAVASCRIPT">
var img_rnd = new Array ("1.gif", "2.gif", "3.gif", "4.gif", "5.gif", "6.gif", "7.gif", "8.gif", "9.gif", "10.gif", "11.gif", "12.gif");
var i = Math.round(Math.random() * 11);
alert(img_rnd[i]);
document.write('<a href="http://www.morsedesigns.com" target="_new"> <IMG SRC="www.morsedesigns.com//bigk/" + img_rnd[i]>');
</SCRIPT>

morse
February 23rd, 2003, 05:07 PM
<SCRIPT LANGUAGE="JAVASCRIPT">
var img_rnd = new Array ("1.gif", "2.gif", "3.gif", "4.gif", "5.gif", "6.gif", "7.gif", "8.gif", "9.gif", "10.gif", "11.gif", "12.gif");
var i = Math.round(Math.random() * 11);
alert(img_rnd[i]);
document.write('<a href="http://www.morsedesigns.com" target="_new"> <IMG SRC="www.morsedesigns.com//bigk/" + img_rnd[i]>');
</SCRIPT>


forgot the php tags ;)

lostinbeta
February 23rd, 2003, 05:53 PM
Still unmatching quotes...


<SCRIPT LANGUAGE="JAVASCRIPT">
var img_rnd = new Array ("1.gif", "2.gif", "3.gif", "4.gif", "5.gif", "6.gif", "7.gif", "8.gif", "9.gif", "10.gif", "11.gif", "12.gif");
var i = Math.round(Math.random() * 11);
alert(img_rnd[i]);
document.write("<a href='http://www.morsedesigns.com' target='_blank'> <IMG SRC='www.morsedesigns.com/bigk/'" + img_rnd[i]+">");
</SCRIPT>

Also you had the target as _new it should be _blank and your moredesigns.com//bigk should only be one forward slash.

CyanBlue
February 23rd, 2003, 06:06 PM
Yup... My bad... :blush:

lostinbeta
February 23rd, 2003, 06:13 PM
And while I am at it, I made some slight adjustments....

<SCRIPT LANGUAGE="JAVASCRIPT">
var img_rnd =
["1.gif",
"2.gif",
"3.gif",
"4.gif",
"5.gif",
"6.gif",
"7.gif",
"8.gif",
"9.gif",
"10.gif",
"11.gif",
"12.gif"]
var i = Math.floor(Math.random()*img_rnd.length);
alert(img_rnd[i]);
document.write("<a href='http://www.morsedesigns.com' target='_blank'> <IMG SRC='http://www.morsedesigns.com/bigk/'" + img_rnd[i] + ">");
</SCRIPT>

I lined the array up like that just because it makes it easier to read and edit then when it is one really long line.
Then I changed the i variable to be a more efficient equation that uses Math.floor bceause arrays start counting at 0, and with Math.round() it is very rare if at all you will get 1.gif in that case. I also multiplied the random number by the length of the img_rnd array so that you won't have to update this equation when you update the array.

I didn't test it, but it should work.

lostinbeta
February 23rd, 2003, 06:31 PM
Wait Wait.... I got a chance to test it, that script doesn't work, I got the quotation wrong (I knew I shoulda tested it!)


<SCRIPT LANGUAGE="JAVASCRIPT">
var img_rnd =
["1.gif",
"2.gif",
"3.gif",
"4.gif",
"5.gif",
"6.gif",
"7.gif",
"8.gif",
"9.gif",
"10.gif",
"11.gif",
"12.gif"]
var i = Math.floor(Math.random()*img_rnd.length);
var addImage = img_rnd[i];
alert(addImage);
document.write("<a href='http://www.morsedesigns.com' target='_blank'> <IMG SRC=http://www.morsedesigns.com/bigk/" + addImage + "></A>");
</SCRIPT>

Works great.

I added a variable for img_rnd[i] because you use it twice, so for easier updating if ever necessary you can just edit that. I also forgot to close the A tag.

morse
February 23rd, 2003, 09:05 PM
Wow Shane thanks man =) Your good at this. Yes, you are. Thanks again man. I know nothing about JS.

And CyanBlue, thanks also!

:) :) :)

lostinbeta
February 23rd, 2003, 11:08 PM
No problem man... JS was my thing before AS as you know :)