Departments
 Info
   Home Page
   Articles
   Software Goodies

 Explanations
   How do they do that?
   The Web in Focus

 Resources
   Web Site Stuff
   Tech Books
   E-Books
   Tech Toys
   Web Hosting
   Links

 Women
   Opinion
   Tech Women
   Women's Studies

 Tech News
   Computer Security
   Databases
   Java
   Linux
   MP3
   PC Software
   Robotics
   Site Owner
   Tech Latest
   Web Development
   XML

 Spread the Word
   Newsletter
   Recommend this Page

 Site Info
   Legal Disclaimer
   Privacy
   Contact

 Lighter Side
   Crazy for Life
   crazy for romance
   Crazy for Kitties
   Crazy for Dogs
   Crazy for Cows

Copyright 2000-2001, hertechnology.com

 
How do they do that?
Generating Random Quotes
var qarray = new Array();
qarray[0] = '"You are never given a wish without<br>also being given the power to make it true.<br>You may have to work for it, however." -- Richard Bach';
qarray[1] = '"A well adjusted person is one<br>who makes the same mistake twice<br>without getting nervous." -- Jane Heard';
qarray[2] = '"We have fallen into the place<br>where everything is music." -- Rumi'

For unimportant historical reasons, the first compartment is number "0". All 12 quotes were added in positions 0 to (and including) 11.

Now for the second tricky bit. We've got to pick one of the quotes in our collection, but which one? We want to pick a different quote each time, too.

One way to do this is to pick the quote randomly. This is done by asking JavaScript to give us a random number. In this case, we tell JavaScript to give us a random number between 0 and 11 (including 11). Once we get the random number from JavaScript, we go into our array and select the quote associated with that number.

The last point to understand is how the browser handles JavaScript. You can use JavaScript code to create information for the web page. That is what we are doing with the random quote generator. The browser doesn't know ahead of time which quote to put on the page. It runs the JavaScript code, gets a random number, then gets the quote associated with that number. Only then can the browser write the quote on the page for you, the reader, to see. Each time you load the page, the browser will run the JavaScript code and pick a different random number, thereby selecting a different quote for the web page.

Final nit: sometimes JavaScript picks the same random number twice in a row (or even 3 times). The more quotes you have to choose from, the less likely the same random number will be chosen in a row.

The use of a container (array) to hold different items and then selecting one of them with a random number is very useful. You can use it to select a random photograph, a random link, a large block of text, or many other things. If you notice something different popping up in a web page in the same location, (like the banner above), some kind of container and random number is the cause.

Tech Books

Tech Toys

More News

  Want to bookmark this site? Press Control-D. (Hold down the control key and the letter D at the same time.)