Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: random text?

  1. #1
    New Coder
    Join Date
    Oct 2009
    Location
    Rizal Province, Philippines
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    random text?

    I have a text file(quotes.txt) with 35+ quotations, separated by a single line break. Is it possible for a DIV to display a random quotation from quotes.txt each time the user visit my home page?

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Javascript can't read/write to/from a file in the server. So you need to use some server side code for this.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Of course it is possible! With only 35 quotes, there is no problem in using Javascript.

    Code:
    <span id = "quote"></span>
    
    <script type = "text/javascript">
    var Quotation = new Array();
    Quotation[0] = "Time is of the essence! Comb your hair.";
    Quotation[1] = "Sanity is a golden apple with no shoelaces.";
    Quotation[2] = "Repent! The end is coming, $9.95 at Amazon.";
    Quotation[3] = "Honesty blurts where deception sneezes.";
    Quotation[4] = "Pastry satisfies where art is unavailable.";
    Quotation[5] = "Delete not, lest you, too, be deleted.";
    Quotation[6] = "O! Youth! What a pain in the backside.";
    Quotation[7] = "Wishes are like goldfish with propellors.";
    Quotation[8] = "Love the river's \"beauty\", but live on a hill.";  // note escape quotes
    Quotation[9] = "Invention is the mother of too many useless toys.";
    
    var Q = Quotation.length;
    var whichQuotation = Math.round(Math.random()*(Q-1));
    function showQuotation(){
    document.getElementById("quote").innerHTML = Quotation[whichQuotation];
    }
    
    showQuotation();
    
    </script>
    "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •