|
Departments Info Home Page Articles Software GoodiesExplanations How do they do that? The Web in FocusResources Web Site Stuff Tech Books E-Books Tech Toys Web Hosting LinksWomen Opinion Tech Women Women's StudiesTech News Computer Security Databases Java Linux MP3 PC Software Robotics Site Owner Tech Latest Web Development XMLSpread 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 CowsCopyright 2000-2001, hertechnology.com |
Generating Random Quotes var qarray = new Array(); 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. |
|