Paul McFedries' Web Home


My JavaScript Scripts

Special Edition Using JavaScript "I'm extremely happy with the purchase I have to say, as it covers the whole gamut of JavaScript — including Regular Expressions, which are not commonly even mentioned — with easy to understand, well commented code examples."
—C. Hopper

You can gain extra control over your Web pages, and also add a few bells for surfers to ring and whistles for them to blow, by including scripts in your pages. These scripts are created with a scaled down version of Java called, appropriately enough, JavaScript.

JavaScript code goes right inside the Web page, just like HTML tags. When a JavaScript-savvy browser (such as Netscape 2.0 and later and Internet Explorer 3.0 and later) accesses the page, the browser executes the JavaScript code and the program does its thing. For example, the program might check the time of day and display an appropriate welcome message, or you could embed a calculator right on the page.

JavaScripts reside between the <SCRIPT> and </SCRIPT> tags and always take the following form:

<SCRIPT LANGUAGE="JavaScript">
<!--
The script commands go here.
//-->
</SCRIPT>
Note the use of HTML comment tags. These ensure that JavaScript-feeble browsers don't try to read the JavaScript commands. (The "//" part of the second comment tag is there to prevent JavaScript from trying to "run" the comment.) Where you store the script depends on what it does:
  • If the script writes text to the page, you position the script where you want the text to appear.
  • Otherwise, you position the script between the </HEAD> and <BODY> tags.
Below you'll find a collection of links to some JavaScript programs that I wrote myself. They enable you to set up things like cookies, status bar messages, and more. Most of these scripts were written as examples for my books or as extra goodies for the readers of The Complete Idiot's Guide to Creating a Web Page. Please feel free to "steal" the code for use on your own pages. If you feel so inclined, I'd appreciate it if you mentioned that I'm the author of the code (a link to my home page—http://www.mcfedries.com/—would be nice, too).

Scripts for Forms:

* Restricting the Number of Form Submissions
This script uses cookies to restrict the number of times a user can submit a form.
* Disabling Form Controls
How to disable a form control until a check box is activated.
* Calculating Order Totals
How to calculate and display running totals in an order form.
* Check Box Restrictor
This script prevents the user from selecting more than a specified number of check boxes.
* Form Confirmer
This program shows the user their form data before submitting it.
* Making Form Fields Mandatory
This page shows you how to use JavaScript to force your users to enter data into a field.
* Mortgage Minder
Calculate the monthly payment for a mortgage or loan.
* Payment Prognosticator
This JavaScript applet can calculate the future value of a series of regular deposits to an investment.
* Add It Up
This script assigns "weights" to various items in several list boxes and then supplies a total based on the items selected by the user.
* The Geek Rater
Find out how you rate on the geek scale.

Scripts for Mouseovers:

* Simple Mouseover Script
A script that changes one image to another when you put the mouse over the image
* Preventing Internet Explorer Errors
A mouseover script that prevents IE errors.
* Pre-loading Multiple Images
A mouseover script suitable for multiple images.
* Simpler Multiple Mouseovers
A simplified script for multiple images.

Scripts for Status Bar Messages:

* Yer Basic Scrolling Status Bar Message.
A message that scrolls in from right-to-left.
* Single Scrolling Status Bar Message.
A message that scrolls in from the right and then stops.
* Bouncing Status Bar Message.
A message that scrolls in from the right and bounces off the left edge.
* Multiple, Scrolling Status Bar Messages.
Two or more messages that scroll right-to-left, in order.
* Multiple, Random, Scrolling Status Bar Messages.
Two or more messages that scroll right-to-left randomly.
* Controlling the Scrolls
Controlling the number of times the status bar message scrolls

Scripts for Dates:

* Displaying the Date and Time
A script that displays the current date and time.
* The Days Between Two Dates
This script presents a function that calculates the number of days between any two dates.

Scripts for Frames:

* Getting Out of Someone Else's Frames
A simple script that ensures your page doesn't get displayed inside another site's frames.
* Reframing a Deframed Page
The scripts in this example show you how to force "deframed" pages to load within a frame.

Scripts for Math and Finance:

* Rounding a Value to a Specified Number of Decimal Places
This page shows a function that rounds a value to any number of decimal places.
* Calculating Order Totals
How to calculate and display running totals in an order form.
* The Geek Rater
Find out how you rate on the geek scale.
* Mortgage Minder
Calculate the monthly payment for a mortgage or loan.
* Payment Prognosticator
This JavaScript applet can calculate the future value of a series of regular deposits to an investment.
* Add It Up
This script assigns "weights" to various items in several list boxes and then supplies a total based on the items selected by the user.

Miscellaneous Scripts

* Image Cycler
Here's a bit of JavaScript that cycles through a series of images..
* Preventing Right-Clicks
This script offers a method that prevents users from right-clicking your page, which can help thwart image theft..
* JavaScript Cookies
This page shows you how to use JavaScript to store and read cookies.
* Detecting the User's Browser
Two scripts that return information about the user's Web browser:
* Setting Up a JavaScript Timer
This script creates a JavaScript timer that counts down a certain number of seconds and then does something.
* JavaScript Random Sounds
This page contains a JavaScript that plays a random sound each time you load the the page.
* Random Word Generator
A script that generates random words from a list.
* Navigating the Top and Bottom of a Page
This script shows you how to implement buttons within a frame that enable the user to navigate to the top and bottom of a page in another frame.
* Passing Parameters From One Page to Another
Method I: Using a modified <A> tag
Method II: Using a cookie
* Creating a Drop-Down List of Links
These pages demonstrate and explain how to use JavaScript to set up a drop-down list of links similar to the one I have at the top of each of my pages:

Copyright © 1995-2008 Paul McFedries and Logophilia Limited