• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

15. Dynamic Content Applications > 15.1. Displaying a Random Aphorism

Displaying a Random Aphorism

NN 4, IE 4

Problem

You want a random choice from a library of famous quotes to appear in a location on your home page.

Solution

The backward-compatible (to Version 4 browsers) solution is to use the document.write( ) method to insert your quotation element while the page loads. But first, start by creating an array of sayings in the head section. Each array entry is a custom object containing the saying and author’s name:

var quotes = new Array( );
quotes[quotes.length] = {quote:"One should eat to live, and not live to eat.", 
                          author:"Moliere"};
quotes[quotes.length] = {quote:"For man is man and master of his fate.", 
                          author:"Tennyson"};
quotes[quotes.length] = {quote:"History is more or less bunk.", 
                          author:"Henry Ford"};
quotes[quotes.length] = {quote:"You should never have your best trousers on when you 
                          turn out to fight for freedom and truth.", 
                          author:"Ibsen"};
quotes[quotes.length] = {quote:"It is vain and foolish to talk of knowing Greek.", 
                          author:"Woolf"};
...

PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint