Click to See Complete Forum and Search --> : getting one liners randomly chosen from a set
mikeycorn
Oct 24th, 2002, 03:39 AM
Real newbie web design question:
I'm working with dreamweaver 4 and I can't figure where to look in the help to find out how you set it up to where I have a group of different one-liners (a set of single lines of text) and have a different one randomly selected and displayed each time the page is hit.
Anyone?
Rick Bull
Oct 24th, 2002, 05:34 AM
This is one way to do it using JavaScript:
<script type="text/javascript"><!--
function randomNumber(maxValue, minValue) {
//If minValue/maxValue is missing make it/them 0/1
if (minValue == null) minValue = 0;
if (maxValue == null) maxValue = 1;
//Return the number
return (Math.round(maxValue * Math.random()) + minValue);
}
//List of all lines wanted - add your own
var oneLiner = new Array(
'Line 1',
'Line 2',
'Line 3',
'Line 4'
);
//Output the line
document.write(oneLiner[randomNumber(oneLiner.length-1)]);
document.close();
//--></script>
Just add the lines you want to be written to the array, seperated by commas, and encased in single or double quotes.
mikeycorn
Oct 24th, 2002, 10:36 AM
Wow! That is cool. Thanks Rick Bull. (Damn, I'm afraid that little snippet right there has me thinking it's time to move beyond the comfy confines of VB and start learning some new tricks.)
mikeycorn
Oct 24th, 2002, 11:11 AM
How do I get single quotes inside the single quotes???
I figured maybe the percent sign and the ascii code.
As an example, I tried 'It%39s only me.' but it didn't work.
Rick Bull
Oct 24th, 2002, 05:01 PM
You can either escape it with \ like so:
document.write('single quote \\'');
or use double quotes:
document.write("single quote '");
CornedBee
Oct 24th, 2002, 05:08 PM
In JavaScript ' and " are completly interchangeable, except that the starting and ending sign of one particular string must be the same (you can't do "this is a string')
mikeycorn
Oct 24th, 2002, 06:17 PM
Awesome, thanks guys.
If you want to check out the little spice you just helped me add to my web page, check out the slogans at the bottom of the page:
www.vellosoft.com
And be sure to hit refresh a bunch of times (some of them are funnier than others.)
Rick Bull
Oct 25th, 2002, 07:11 AM
You think you have enough catch-phrases in there, there must be about 50 :eek:
mikeycorn
Oct 25th, 2002, 12:00 PM
Actually, that nice JavaScript you provided is dipping into an array of 367 slogans I had put together for a quiz on corporate slogans. My favorite are the one's that make absolutely no sense, like "Vellosoft. The Other White Meat." or "Vellosoft. Just for the Taste of It."
:D
Rick Bull
Oct 25th, 2002, 12:18 PM
Ah right, it all makes sense now. :p
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.