Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Text Generator and CSS 1

Status
Not open for further replies.

danielh68

Technical User
Jul 31, 2001
431
US
Hi everyone.

I grabbed this code from a javascript resource and I'm trying to tweak it to accomodate my text. Unfortunately, it doesn't work when I implement a css style. I need the css style implemented to maintain the visual continuity of the site. Anyhow here's the code:
--------------------------------------------------------
function GetRandom(start,end)
{
var range = end - start + 1;
var result = start + Math.floor(Math.random()*range);
return result;
}
</script>

<script language=&quot;JavaScript&quot;>

var choice = GetRandom(1,4);
if (choice==1)
document.write(&quot;<p class=&quot;heading&quot;>Broadband Store Grand Opening</p>&quot;);
else if (choice == 2)
document.write(&quot;<b>Tip:</b> Never spit into the wind.&quot;);
else if (choice == 3)
document.write(&quot;<b>Tip:</b> Look before you leap.&quot;);
else if (choice == 4)
document.write(&quot;<b>Tip:</b> Walk tall and carry a big stick.&quot;);
</script>
---------------------------------------------------------
As you can see, I have a css in the first write command. If I remove the css so that it reflects the following write command it works. Any suggestions?

Thanks,
Dan
 
This should handle what you want:

var message;
switch (GetRandom(1,4))
{
case 1: message='Broadband Store Grand Opening'; break;
case 2: message='<b>Tip:</b> Never spit into the wind.'; break;
case 3: message='<b>Tip:</b> Look before you leap.'; break;
case 4: message='<b>Tip:</b> Walk tall and carry a big stick.'; break;
}

document.write('<p class=&quot;heading&quot;>' + message + '</p>');
 
Thanks Trollacious. It works great! There's one other fact that I forgot to mention and that is I need two classes implemented. For example, I took the code you provided and changed it to this:
-------------------------------------------------------
var message;
switch (GetRandom(1,4))
{
case 1: message1='Broadband Store Grand Opening'; break;
message2='This month you are invited...'; break;
case 2: message1='<b>Tip:</b> Never spit into the wind.'; break;
case 3: message1='<b>Tip:</b> Look before you leap.'; break;
case 4: message1='<b>Tip:</b> Walk tall and carry a big stick.'; break;
}

document.write('<p class=&quot;heading&quot;>' + message1 + '</p>'); ('<p class=&quot;text2&quot;>' + message2 + '</p>');</script>
-----------------------------------------------------------
As you noticed in case 1, I added message 2. I thought this would work, apparently I'm wrong. Again, any suggestions is much appreciated.

Thanks,
dan
 
Look closely at the first case in the switch statement to see your error.
 
Hi trollacious,

I'm looking, but I'm not seeing. Oh wait, because I need to change the variable to var message1, message2?

Thanks,
Dan
 
The original problem was double quotes inside the document.write. You could have either done this:
Code:
document.write(&quot;<p class='heading'>Broadband Store Grand Opening</p>&quot;);
using single-quotes

or
Code:
document.write(&quot;<p class=\&quot;heading\&quot;>Broadband Store Grand Opening</p>&quot;);
escape the double-quotes so that they will be passed as simple text characters and not string delimiter characters.


The second problem was too many break statements. The switch statement syntax is:
Code:
switch variable
{
  case <value 1>:
         statement;
         statement;
         break;
  case <value 2>:
         statement;
         statement;
         break;
}
Hope that helps.

Einstein47
(How come we never see the headline, &quot;Psychic Wins Lottery&quot;?)
 
Works! Thanks everybody for you expertise and help. -- Dan
 
correction: YOUR expertise and help. Typing too fast, I guess. Thanks, again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top