Friday, April 29, 2011

How can I rotate a line of text each page refresh?

Right now, Reddit is down. Every time you go to it, it displays a logo and below it a funny message. Every time you refresh the page it displays a new message.

alt text

How could I write something simple that ~rotates~ (not random) a line of text on the page every refresh?

I'm using ASP.NET MVC but if there is a JavaScript solution to this problem that is fine too.

From stackoverflow
  • Here's an example of how to do it in javascript:

    http://www.webdevelopersnotes.com/tips/html/random_text_display_using_javascript_1.php3

    Mithrax : Sorry, but I tried to specify in my post i'm ~not~ looking for random. Thanks anyway though
  • This is how you could write it:

    For javascript, simply use the link posted by Andy White and replace the call to Math.floor(7*Math.random()) to a get/set cookie value. You can find ready-to-use javascript functions to read and write cookies on google.

    For asp.net mvc, the code won't differ a lot, you just have to look for functions to easily manipulate cookies as they are built-in.

  • Load your message into an array or some sort of collection that is accessible by index.

    Something like (i did not verify all syntax):

    string getQuote(){
     //Load collection of lines
      ArrayList quotes = getQuotesData();
      int quoteCount = quotes.Count;
    
     // Set session or application variable to some number
      int counter;
      if( !Integer.parse( Session("counter"), counter ) ) { counter = 0; }
      if( counter > quoteCount ) { counter = 0; }
    
      return quotes[counter];
    }
    

    Also, I'm sure something similar is possible in javascript.

0 comments:

Post a Comment