Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Create reusable routines in JavaScript

Tired of rewriting everything? Here's how to build a component library for JavaScript and reuse common routines -- simply by cutting and pasting code

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 3 of 6

Starter library: Some plug-and-play functions you can use

During the past year of writing JavaScript programs, I've discovered that about 85 percent of my projects have involved commonly used code. I've developed an extensive library of this code, some of which is presented here. Feel free to snatch any or all of the functions demonstrated below. And of course you're free to improve upon these functions, and make them more efficient.

For the sake of simplicity, the actual code for each routine is not presented in the body of this column. Instead, it is part of the example provided with each routine. Click on the link for each example to see it demonstrated. You can view the code for the routine in Netscape Navigator by choosing the following menu items: "View," "Document Source."

Note: These routines were tested with Navigator versions 2.02 and 3.0 under Windows 95. JavaScript can and does exhibit unstable behavior when used on different browser platforms and versions. If you plan to use any of the following code in a public Web page, be sure to test it with as many JavaScript-capable browsers as you can. Such is the downside of using JavaScript.


allowBrowser
The allowBrowser function lets you quickly and easily determine whether the user's version of Navigator is allowed for your application. This is handy if you need to prevent a user from accessing parts of your page that may cause problems with a particular Netscape platform. You can identify which browsers you want to "allow" by using a letter. You can use the letters singly or in combination if you wish to allow multiple browsers.

Letter symbol Netscape platform
W Windows 9x (such as Windows 95)
w Windows 3.1
N Windows NT
M Mac PPC
m Mac 68K
X X Window


The syntax for allowBrowser is:

var Ret = allowBrowser (AllowString);


AllowString above is one or more characters that represent the browsers you wish to allow. The function returns Boolean true if the user's browser is among those listed in allowString; false if not.

This example displays true if you are using the Windows 9x version of Navigator; false for any other.

var Ret = allowBrowser("W");
alert (Ret);


Feel free to mix and match browsers if you wish to allow for a variety of them. Here are some examples:

Example function call Allows
allowBrowser("WwMm") Windows 3.1, Windows 9x, Mac PPC, Mac 68K
allowBrowser("XWN") X Window, Windows 9x, Windows NT
allowBrowser("wX") Windows 3.1, X Window


One of the main purposes of the allowBrowser function is to check the user's browser so that you can avoid any platform-specific bugs. But you can use it for other applications. For example, you can check for the Windows versions ("WwN"), and display a "Welcome Windows Users" message at the top of your page.

allowInString
The allowInString function tests every character in a string against a set of "allowed" characters. If the function finds a character that is not allowed, it returns false; otherwise it returns true. The allowInString function primarily is used for form or input validation. The syntax for the allowInString function is:

var ret = allowInString (InString, RefString);


InString is the string you want to test. RefString is the list of acceptable characters. The function returns Boolean true if InString does not contain any invalid characters; otherwise it returns false.

This example displays false: The input string contains invalid characters (the dollar sign is not allowed):

var TestThisString = ",987,239.28";
var ValidString = "01234567890.,";
var Ret = allowInString (TestThisString, ValidString);
alert (Ret);


allowNotInString
The allowNotInString function is the logical inverse of the allowInString function above. It tests every character in a string against a set of "disallowed" characters. If the function finds any character contained in the disallowed list, it returns false; otherwise it returns true. One of the primary uses of the allowNotInString function is for form or input validation. The syntax is:

var ret = allowNotInString (InString, RefString);


InString is the string you want to test. RefString is the list of unacceptable characters. The function returns Boolean true if InString does not contain any invalid characters; otherwise it returns false.

This example displays true: The input string does not contain any invalid characters. The example tests whether a CompuServe ID number contains 8, 9, or a comma, which is not allowed (the comma is allowed within CompuServe, but not when specifying a CompuServe address through the Internet).

var TestThisString = "71333.1776";
var ValidString = "89,";
var Ret = allowNotInString (TestThisString, ValidString);
alert (Ret);


filenameOnly
The filenameOnly function returns just the filename (minus the path) of a fully qualified URL. The syntax of the filenameOnly function is:

var Ret = filenameOnly(InString);


InString is the fully qualified URL you want to use, such as:

http://mydomain.com/dir/homepage.html 


The function returns "homepage.html." If you wish to return the filename of the current document, use location.href for InString. The function returns the filename only of the fully qualified URL.

This example returns the filename only of the current document.

var Ret = filenameOnly(location.href);
alert(Ret);


isAlphabeticString
The isAlphabeticString function determines if all the characters in a string are alphabetic characters (not numbers or punctuation). Here is the syntax for the isAlphabeticString function:

var Ret = isAlphabeticString (TestString)


TestString is the string you want to test. The function returns a numeric value: 1 if the string is all-alphabetic; 0 if it is not.

This example displays a specific message telling you the result of the text you typed.

var Ret = prompt ("Type some characters ");
if (isAlphabeticString(Ret))
    alert ("All characters were alphabetic");
else
    alert ("At least one character was not alphabetic");


isNumberString
The isNumberString function determines if all the characters in a string are numbers. The syntax is:

var Ret = isNumberString (TestString);


TestString is the string you want to test. The function returns a numeric value: 1 if the string is all numbers; 0 if it is not.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (1)
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources