JavaScript City

Random Quote Alert

This script will show an alert with a random quote from a set of 10 when the viewer clicks a button.

Script Code

Grab a copy of the file below and place it in the same directory as the Web page on which you will use the script (if you wish to place the script file in a different directory you will need to make the necessary adjustments to the src attribute of the script tag that calls it).

salert3.js

The code below should be placed between the <body> and </body> tags where you want the button to appear.

Example

A working sample of the script is shown below.

Options

You can change the text of the quotes by opening the salert3.js file and editing the bold (strong) text on the following lines of code:

quote[0]="You clicked that button, didn\'t you?";
quote[1]="These aren\'t the \'droids you\'re looking for.";
quote[2]="No soup for you!";
quote[3]="D\'oh!";
quote[4]="If you click that button one more time, you\'ll get another quote.";
quote[5]="It\'s all about the Pentiums!";
quote[6]="This is fun, but don\'t quote me on that. D\'oh!";
quote[7]="More than meets the eye!";
quote[8]="Help me Obi-Wan Kenobi. You\'re my only hope.";
quote[9]="I lost on Jeopardy!";

Additional quotes can be added by adding lines for quote[10], quote[11], and so on:

quote[10]="Quote Text";
quote[11]="Quote Text";

If you need to use single or double quotation marks within a quote, you should write them with a backslash before them (i.e. \" and \') in order to avoid JavaScript errors.

Accessibility

This could be made accessible for those without JavaScript by directing the form action attribute to a server side script that will perform the same function (perhaps writing the quote on another page and allowing the viewer to go back). Another way might be to provide an HTML page with the list of quotes for those that can't get the alerts.


eXTReMe Tracker