Welcome to WebmasterWorld Guest from 54.158.36.59

Forum Moderators: open

Message Too Old, No Replies

Random switch between document.write() statements needed

I suck at JavaScript and I am not afraid to admit it

     
1:26 am on Jul 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can someone please do my homework?

All I want is something that says pick a number between 0 and 1. If it is 0 - document.write X, if it is 1 document.write Y.

Easy right? Yeah, not if you suck at JS. ;)

please help.

<!-- hide script

var rannum = Math.round(Math.random() * 1;

if (rannum <1 ) {

document.write('<a href="link.htm" target=\"_blank\">
<img src="ads/X.gif" border="0" vspace="10" alt="X"></a>');
}
else {
document.write('<a href="link2.htm" target=\"_blank\">
<img src="ads/Y.gif" border="0" vspace="10" alt="Y"></a>');
}

// end hiding -->

It's for an external file, if that makes any difference.

thank you.

1:33 am on Jul 27, 2003 (gmt 0)

10+ Year Member



Should the first line read?:-
var rannum = Math.round(Math.random() * 1);
1:38 am on Jul 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey. cool that fixed it. except it is only showing one image every time. not very random. But I think I can fix that.

thank you.

1:45 am on Jul 27, 2003 (gmt 0)

10+ Year Member



Glad that was of some help, I don't know why it is only showing one image - I am hopeless as JavaScript myself, but my first instinct is always to look for missing brackets or semi-colons if something goes wrong! ;)
1:49 am on Jul 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is that is made the variable wrong

the first one is shown is the random number was < 1, well between 0 and 1 it is always greater than or equal.

I changed it to say > 0, which means if it = 0, it selected the other option. :)

 

Featured Threads

Hot Threads This Week

Hot Threads This Month