Random Quote Script

Discussion in 'JavaScript' started by magiscoder, Feb 25, 2010.

  1. #1
    I'm looking to put a random quote script on my new website. I found a very helpful script that does exactly what I want here:

    
    <script language="JavaScript">
    // ==============================================
    // Copyright 2004 by CodeLifter.com
    // Free for all; but please leave in this header.
    // ==============================================
    
    var Quotation=new Array() // do not change this!
    
    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.";
    Quotation[9] = "Invention is the mother of too many useless toys.";
    
    // ======================================
    // Do not change anything below this line
    // ======================================
    var Q = Quotation.length;
    var whichQuotation=Math.round(Math.random()*(Q-1));
    function showQuotation(){document.write(Quotation[whichQuotation]);}
    showQuotation();
    </script>
    
    Code (markup):
    However, I know I'll be adding more quotes to the database, and since this will appear on every page of my website, I don't want to have to update it multiple times. With that being said, is there a way to make a javascript file in my main directory and then link to that on each page, that way I only need to update one file as opposed to all of them?

    Thanks for the help!
     
    magiscoder, Feb 25, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Create a file named for example: random.js

    Containing the following code:

    // ==============================================
    // Copyright 2004 by CodeLifter.com
    // Free for all; but please leave in this header.
    // ==============================================
    
    var Quotation=new Array() // do not change this!
    
    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.";
    Quotation[9] = "Invention is the mother of too many useless toys.";
    
    // ======================================
    // Do not change anything below this line
    // ======================================
    var Q = Quotation.length;
    var whichQuotation=Math.round(Math.random()*(Q-1));
    function showQuotation(){document.write(Quotation[whichQuotation]);}
    showQuotation();
    Code (markup):
    Then in every page of your website where you want the quote/s to appear, add the following line of code (doesn't matter where, but would recommend within your head tags.):

    <script src="random.js"></script>


    Then whenever you want to update the javascript just edit 1 file -> random.js and all the pages would be updated ;)
     
    danx10, Feb 25, 2010 IP
  3. magiscoder

    magiscoder Peon

    Messages:
    157
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Perfect, that's just what I was looking for. Thanks!
     
    magiscoder, Feb 25, 2010 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    this is one horrid little script
     
    dimitar christoff, Feb 25, 2010 IP
  5. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #5
    should be <script type="text/javascript" src="random.js"></script> if you want valid html.
     
    killaklown, Feb 26, 2010 IP
  6. omarabid

    omarabid Well-Known Member

    Messages:
    1,509
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    130
    #6
    omarabid, Feb 27, 2010 IP