1. Javascript deletes spaces before and after a string.
String. Prototype. Trim = function ()
{
Return this. Replace (/(^/S *) | (/S * $)/g ,"");
}
2. Javascript checks whether it is a number (function equivalent to isnan)
Function isnumber (STR)
{
VaR patt =/^/d + (/./d + )? $ /;
Return patt. Test (STR );
}
4. Javascript determines the email address format
Function isemail (STR)
{
VaR patt =/^ ([/W. -]) +/@ ([/W-]) + /.) + ([a-zA-Z0-9] {2, 4}) + $ /;
Return patt. Test (STR );
}
5. Trigger the click event of an object
Function clickit (elementid)
{
VaR ele = Document. getelementbyid (elementid );
Ele. onclick. Call (Ele );
}
6. getbrowsertype ()
Function getbrowsertype ()
{
VaR patt =/Microsoft/I;
If (patt. Test (window. Navigator. appname) Return "ie ";
Patt =/Netscape/I;
If (patt. Test (window. Navigator. appname) Return "FF ";
Return "";
}
8. Add events to objects
Function addobjectevent (objid, eventname, eventfunc)
{
VaR theobj = Document. getelementbyid (objid );
If (window. attachevent) // IE
{
Theobj. attachevent (eventname, eventfunc );
}
Else if (window. addeventlistener) // Firefox
{
Eventname = eventname. tostring (). Replace (/on (. *)/I, '$1 ');
Theobj. addeventlistener (eventname, eventfunc, true );
}
}
Addobjectevent ('the object id', 'onclick', show );
Function show ()
{
Alert ('OK ');
}
Note:
IE: attachevent, detachevent
FF: addeventlistener, removeeventlistener
9. Remove an event from an object
Function removeobjectevent (objid, eventname, eventfunc)
{
VaR theobj = Document. getelementbyid (objid );
If (window. detachevent) // IE
{
Theobj. detachevent (eventname, eventfunc );
}
Else if (window. removeeventlistener) // Firefox
{
Eventname = eventname. tostring (). Replace (/on (. *)/I, '$1 ');
Theobj. removeeventlistener (eventname, eventfunc, true );
}
}
Removeobjectevent ('the object id', 'onclick', show );
Function show ()
{
Alert ('OK ');
}
11. hexadecimal conversion
/*
* Convert any hexadecimal value to a decimal value.
* The num parameter indicates the number of data to be converted.
* The STR parameter indicates the hexadecimal information to be converted. For example, the standard binary format is "01" and the standard octal format is "01234567"
*/
Function todec (Num, STR ){
VaR r = 0;
For (VAR I = num. Length-1, j = 0; I> = 0; I --, J ++ ){
VaR T = Str. indexof (Num. charat (I ));
If (t =-1) Return "error ";
R + = T * Math. Pow (Str. length, J );
}
Return R;
}
/*
* 10-digit conversion to any-digit Conversion
*/
Function carrybit (Num, STR ){
VaR r = "";
If (Str. length> 1 ){
If (Num> = Str. Length ){
R + = carrybit (math. Floor (Num/Str. length), STR );
R + = carrybit (Num % Str. length, STR );
} Else {
R = Str. substr (Num, 1 );
}
}
Return R;
}
12. Format money
Alert (formatmoney ('192. 100 '));
// $123,456,789.13
Function formatmoney (strnumber)
{
If (! Isnumber (strnumber) return strnumber;
VaR nNumber = (number (strnumber). tofixed (2 );
Strnumber = nNumber. tostring ();
VaR decimalpart = "";
VaR intpart = "";
If (strnumber. indexof (".") =-1) // No.
{
Decimalpart = "";
Intpart = strnumber;
}
Else // yes.
{
Decimalpart = strnumber. substring (strnumber. indexof ("."));
Intpart = strnumber. substring (0, strnumber. indexof ("."));
}
Intpart = intpart. Replace (/^ (/D *) $/, "$1 /,");
VaR Re =/(/d) (/d {3 }/,)/;
While (Re. Test (intpart ))
{
Intpart = intpart. Replace (Re, "$1/, $2 ");
}
Intpart = intpart. substring (0, intpart. Length-1 );
Return "$" + intpart + decimalpart;
}
Function isnumber (STR)
{
VaR patt =/^/d + (/./d + )? $ /;
Return patt. Test (STR );
}