JavaScript
Article
By Sam Deering

A jQuery Snippet for Displaying a Random Quote on Page Load

By Sam Deering

This is a simple jQuery code snippet that will display a random quote on your web page every time it loads. This could be useful for displaying different testimonials on your web page every time it is loaded, with a new one displayed every time.

var a = Math.random() + ""
var rand1 = a.charAt(5)
quotes = new Array
quotes[1] = "QUOTE #1"
quotes[2] = "QUOTE #2"
quotes[3] = "QUOTE #3"
quotes[4] = "QUOTE #4"
quotes[5] = "QUOTE #5"
quotes[6] = "QUOTE #6"
quotes[7] = "QUOTE #7"
quotes[8] = "QUOTE #8"
quotes[9] = "QUOTE #9"
quotes[0] = "QUOTE #10"
var quote = quotes[rand1]
alert("

" + quote + "

");
  • Kenny Wright

    I have a quick question about the JQuery script that displays random quotes. I can I get this script to display more than 10 quotes? I work in an academic library and a few of my co-workers and I have been handed the task of creating an education game for new students to play as a part of an introductory to the library and the school. We have settled on creating a “BINGO” like game, and we are considering creating an online version; since our library houses materials for various technical areas, including Computer Science. Our current idea is to have a “Game Administrator’s page,” were we can randomly show facts about the library, while the students use a drag-and-drop option on their assigned BINGO boards.

    • jquery4u

      Hi Kenny,

      In reply to your question, yes you can specify any number of quotes/facts you like.

      quotes[0] = “QUOTE #10” actually references quote 0 as arrays start at zero in JavaScript. So quotes[10] = “QUOTE #10” would be quote 10, So quotes[11] = “QUOTE #11” would be quote 11 and so on. Arrays have no limit.

      Hope this helps you.

      Cheers
      Sam

  • MickeyNotD

    I like this. Can this be triggered by clicking a button? Each time the button is clicked, a new quote shows up? Thanks.

  • Vic

    alert(“YOUVE BEEN INFECTED BY A COMPUTER VIRUS”);

Recommended
Sponsors
Get the latest in JavaScript, once a week, for free.