homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

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)

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)

Should the first line read?:-
var rannum = Math.round(Math.random() * 1);


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

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)

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)

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

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
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