dynamic text on main page

Discussion in 'Web Design and Development' started by aaronw1986, Jul 14, 2009.

  1. aaronw1986 macrumors 68030

    Joined:
    Oct 31, 2006
    #1
    Hi all. I tried searching, but had trouble finding something relevant. How would I go about having different quotations (4-5) rotate which one is displayed on the main page of a site when it is loaded. The lower the language the better, is this possible with HTML/CSS? Any help would be greatly appreciated.
     
  2. BertyBoy macrumors 6502

    Joined:
    Feb 1, 2009
    #2
    PHP out of the question ? or JavaScript ?

    You could store the key of the last quotation displayed to the user in their cookie, or session details, and modify it every time they load the page.
     
  3. Darth.Titan macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #3
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    Can't be done with HTML/CSS alone as they are not dynamic. You could use JavaScript or PHP. JavaScript is the lower of the levels, but requiring it will mean visitors who have JavaScript disabled (like me) won't see the quote, unless you have a default one in place. PHP would be seen by everyone though.

    Here's the quick and dirty though in JavaScript. The same idea can be used in PHP if you want.

    PHP:
    var quotes = [
      
    "Hesitation kills!",
      
    "One is the loneliest number.",
      
    "With much money, comes great sadness."
    ];
    var 
    quote Math.floor(quotes.length Math.random());

    function 
    LoadQuote() {
      
    document.getElementById('quote').innerHTML quotes[quote];
    }
    window.onload LoadQuote;
     

Share This Page