Common JavaScript
Don't waste your time re-inventing the wheel as you develop your own 'in-house'
JavaScript routines. Instead, make full use of this very helpful collection of
JavaScript routines that I have evolved during by web development career. Many
of these routines have been developed from scratch, others have been modified
from free examples available on the web and others may well be complete copies
of other peoples work. Since this set of routines is being provided totally FREE,
I have made no attempt to track down the origins of each routine. This page provides
a brief synopsis of each routine.
The common JavaScript routines exist
in the file common.js and are made available
by inserting the following statement into the <HEAD> section of your web
page:
<script language="javascript" SRC="common.js"></script>
| Function | Description |
|
ucFirst |
Returns a capitalised version of the supplied string. 1st letter upper case, all others lowercase |
|
Trim |
Returns the supplied string after removing all leading and trailing spaces |
| stripHTMLTags | A simple routine to strip HTML tags from supplied string. It's not very clever but it's quite useful. Everything between each "<" and the subsequent ">" is ignored hence it could get confused with any javascript comparisons or comments that contain comparisons. |
|
ReadableName |
Returns a more readable version of the supplied
string. It converts the underscore character to a space and capitalises each word
within the name. |
|
isBlank |
Checks whether the supplied string is blank and returns a boolean. Blank is interpreted as any combination of spaces, linefeeds and/or tab characters. |
|
isValidEmail | Checks
whether the supplied string is a valid email syntax and returns a boolean. General
format checked to be: |
|
isValidTime |
Checks the supplied string to be a valid time value of the format: hh:mm[:ss]. Hours, mins and Seconds are also checked for legal values. |
|
isValidNumber |
Checks the supplied string to be a valid integer |
|
isValidInterval |
Checks the supplied string to be a valid interval
of the format: |
|
IsValidDate |
Checks the supplied string to be a valid date value of the format: dd-mmm-yyyy. Days, months and years are also checked for legal values. |
|
ConvertToJscriptDate |
Converts a general format date dd-mmm-yyyy into a JavaScript Date format. |
|
isValidDateTime |
Checks the supplied string to be a valid date/time value of the format: dd-mmm-yyyy hh:mm[:ss] |
|
isEarlierOrEqual |
Compares two dates and returns true if first date is earlier or equal to the second date, otherwise false. |
|
isTimeEarlierOrEqual |
Compares two times and returns true if first time is earlier or equal to the second time, otherwise false. |
|
LeapYear |
Returns true if the supplied year number is a leap year otherwise false. Leap years are those years divisible by 4 (but not divisible by 100 unless divisible by 400). |
|
frmValidateCommon |
A general purpose form validation routine which takes
an HTML form as its parameter. It loops through all text fields on the form and
verifies that they contain data (unless the optional attribute is set to true
for the field). Other attributes will force other validation checks. Supported
attributes are: date datetime time interval numeric,min,max JavaScript should be used to set the relevant attributes, prior to invoking this routine. onclick ='frm.timestamp.datetime=true; frm.comment.optional=true; return frmValidateCommon(this)' |
|
SetSelections |
This function will highlight the entries in the supplied option list field that exist in the supplied comma separated list of values. e.g. Would highlight the items named Jan and Mar in the combo named monthCombo. Note: the effect of setting multiple values for a single level (pulldown) optional field are undefined and should not be relied on. |
| InsertIntoList | InsertIntoList(Combo,Text,Value,Pos); Inserts the supplied Text and Value as a new element in the specified Option List/Combo. The list is ordered alphabetically. The Pos parameter is optional and allows you to specify which position to use. The function returns the position of the new element. |
| AppendToList | AppendToList(Combo, itemText, itemVal) Use this one for lists which aren't in alphabetical order, otherwise use the InsertIntoList routine. I find this routine useful where I need a list of items that may need to be manually reordered by the user (e.g. prioritisation of items). The ShiftListSelections can then be used to move items up and down in the list. |
| DeleteFromList |
DeleteFromList(Combo,Pos) |
| MoveSelectedListItems | MoveSelectedListItems(srcCombo,
destCOmbo, doSort, IntSort, UseVals) Moves the currently selected items from the source option list into the destination option list. If the doSort parameter is set to true, the dest list will be sorted according to the other supplied paramaters (see SortList for details). Note: This routine is much faster than lots of individual InsertToList calls and should be used for large option lists. |
| SortList | SortList(Combo,
IntSort, UseVals) Sorts the supplied option list alphabetically based on its displayed values. If you need a numerical sort, you can set the IntSort parameter to true. If you need to sort the option list based on its underlying value (as opposed to the displayed value), then set the UseVals parameters to true. |
| ShiftListSelections | ShiftListSelections(Combo,Down)
This routine will shift all the items currently selected in the list up one place (or down one if Down=true). It is expected to be used with multi line option lists where users need to manually select the order of the items within the list. |
|
NewWindow |
NewWindow(url,title,width,height,scrollbars,resize) |
The following
routines are actually generated for you by MacroMedia products, e.g. Dreamweaver
and Fireworks, but since a great many sites make use of them I've included them
in common.js for completeness.
| Function | Description |
|
MM_displayStatusMsg |
Standard MacroMedia Function. Displays the supplied string on the browser status bar. |
|
MM_findObj |
Standard MacroMedia Function. Used by other MM routines when trying to access objects within the document model.. |
|
MM_swapImage |
Standard MacroMedia Function. Swaps the document image of the specified name with an alternative image. Used for rollovers. |
|
MM_swapImgRestore |
Standard MacroMedia Function. Restores a swapped image for its original value. Used for rollovers. |
|
MM_preloadImages |
Standard MacroMedia Function. Invokes as part of a pages onload initialisation to preload images prior to displaying the page. This speeds up image rollovers, since the images are already held in the cache. |
|
MM_showHideLayer |
Standard MacroMedia Function. Allows HTML layers (created using the <DIV> tag) to be hidden and/or made visible. Useful for displaying related information on a page when a user clicks on an item or moves over an item. |