Uncle Jim's JavaScript Examples
|
| addSpace customSplit DeleteCookie formatDecimal formatValue GetCookie isEmail isURL isW ltrim makeArray numCheck ParseCookies replace rtrim SetCookie trim | Listed to the left is collection of custom functions for writing and creating JavaScript. I have indexed them for convenience. To print the full listing, just view the source and copy into your Word Document (Too Large for NotePad). I thought it was handy and easy for me to get to. I hope you can use them as well. Each one is broken down into the following: Function , Syntax , Description , Examples , Code This way, you can highlight the SourceCode and copy it directly to your sever or page, if desired. | ![]() |
stringValue is the string which you want to add the space to.
numlenght is the length of the return string, i.e. add space/s to the string to make it "numlength" long. Default "numlength" is 10.
The following returns " 123.45".
addSpace("123.45", 10);
addSpace("123.45");
strValue is the string to be splited with separator as the delimeter. After spliting, array of strings are stored in new "Array" object, strArrayName.
This function will return the length of the array created.
var strvalue = "abc##123##zzz##$$$"; var returnArraySize = customSplit(strvalue, "##", "NewArray"); The above will create the following: NewArray[0] has value "abc" NewArray[1] has value "123" NewArray[2] has value "zzz" NewArray[3] has value "$$$" returnArraySize has value "4"
cookieName is the cookie that you want to delete.
getCookie functions.
number is the floating point number which will be formatted.
boolean is used to decide whether add "0" at the end of the floating point number or not.
decimal is how many decimal point you wnat. (Default is 2)
formatDecimal("123.2333", true, 2); will return "123.23".
formatDecimal("123", true, 2); will return "123.00".
formatDecimal("123", false, 2); will return "123".
formatDecimal("123.2", true, 2); will return "123.20".
formatDecimal("123.2", false, 2); will return "123.2".
formatDecimal("123.456", true, 2); will return "123.46".
formatDecimal(".235", true, 2); will return "0.24".
formatDecimal("0.9999", true, 2); will return "1.00".
formatDecimal("0.9999", false, 2); will return "1".
formatCurrency function.
argvalue is the number which will be formatted.
format is the format of the result.
And formatDecimal function is needed.
formatValue(1223.434, "$##,###.##") will return "$1,223.43" formatValue(1223.43, "$##,###.##") will return "$1,223.43" formatValue(1223., "$##,###.##") will return "$1,223.00" formatValue(1223, "$##,###.##") will return "$1,223.00" formatValue(23., "$##,###.##") will return "$23.00" formatValue(23.3, "$##,###.##") will return "$23.30" formatValue(124343423.3, "$###,###,###.##") will return "$124,343,423.30"
formatDecimal function.
CookieName is the cookie whose value that you want to get.
If the document.cookie contains:
USER_ID=abc
USER_GP=
// variable "userid" has value "abc".
var userid = GetCookie("USER_ID");
// variable "usergp" has value "".
var usergp = GetCookie("USER_GP");
// variable "userpw" has null value.
var userpw = GetCookie("USER_PW");
testValue is the value that you want to check.
It is better you use trim to remove both the leading and the trailing space/s before you pass the value to this function.
The following return "true".
isEmail("abc@some.host");
The followings return "false".
isEmail("abc");
isEmail("abc@somehost");
isEmail("abc@.some.host");
isEmail("abc@some.host.");
testValue is the value that you want to check.
The followings will return "true".
isURL("http://some.host");
isURL("http://some.host/");
isURL("http://some.host/dir");
isURL("http://some.host/dir/");
isURL("http://some.host/dir/htmlfile");
isURL("http://some.host:123");
isURL("http://some.host:123/");
The followings will return "false".
isURL("http://.some.host/");
isURL("http://some.host./");
isURL("http://some.host:/");
isURL("http://some.host:.123/");
isURL("http://some.host:123./");
isURL("http://some.host:123./dir";
isURL("http://");
isURL("htp://");
testValue is the value that you want to check.
The followings will return "true".
isW("abc123")
isW("ABC123")
isW("aBc_123")
The followings will return "false".
isW("abc 123")
isW("@#+=")
isW("! Abc")
stringValue is the string which the leading space/s will be removed.
The following will return "abc ".
ltrim(" abc ")
The following will return "a b c ".
ltrim(" a b c ")
intArraySize is the length of the array created.
The following will create an array with 3 in length. newArray = new makeArray(3);
testValue is the value that you want to check.
The followings will return "true".
numCheck("1234")
numCheck("0123")
The followings will return "false".
numCheck("abcd")
numCheck("a123")
numCheck("123a")
numCheck("12.3")
There are few ways to save the cookie data, but I found that this is the way which works in both Netscape3.0 and Internet Explorer3.0.
If the server returns the following cookie: Set-Cookie: Cookie1=Value1; Cookie2=Value2 After you call the ParseCookies(). parseCookies(); "document.Cookie_Cookie1.value" will contains value "Value1"; "document.Cookie_Cookie1.name" will contains value "Cookie1"; "document.Cookie_Cookie2.value" will contains value "Value2"; "document.Cookie_Cookie2.name" will contains value "Cookie2";
stringValue is the string which has all X will be substituted by Y.
The following will return "abcABCdefgh".
replace("abc123defgh", "123", "ABC");
stringValue is the string that you want to remove its trailing space/s.
The following will return "abc".
rtrim("abc ")
The following will return "a b c".
rtrim("a b c ")
The following will return " abc".
rtrim(" abc ")
For more information about cookie, see Netscape's Cookie Specification at http://home.netscape.com/newsref/std/cookie_spec.html.
SetCookie("Cookie1", "Value1", null, "/");
SetCookie("Cookie2", "Value2", new Date(), "/");
document.cookie will have value "Cookie1=Value1; Cookie2=Value2".
stringValue is the string which the leading and the trailing space/s will be removed.
The following will return "abc".
trim(" abc ")
The following will return "a b c".
trim(" a b c ")
ltrim, rtrim functions.