Common JavaScript code

Source: Internet
Author: User
Tags modulus
========================================================== ===
// Date comparison
========================================================== ===
*/
Date. Prototype. Compare = function (objdate)
{
If (typeof (objdate )! = "Object" & objdate. constructor! = Date)
{
Return-2;
}

 

VaR d = This. gettime ()-objdate. gettime ();

 

If (D> 0)
{
Return 1;
}
Else if (D = 0)
{
Return 0;
}
Else
{
Return-1;
}
}

 

/*
========================================================== ===
// Format the date
========================================================== ===
*/
Date. Prototype. format = function (formatstr)
{
VaR STR = formatstr;

 

STR = Str. Replace (/YYYY | yyyy/, this. getfullyear ());
STR = Str. Replace (/yy | YY/, (this. getyear () % 100)> 9? (This. getyear () % 100). tostring (): "0" +

(This. getyear () % 100 ));

 

STR = Str. Replace (/MM/, this. getmonth ()> 9? This. getmonth (). tostring (): "0" + this. getmonth ());
STR = Str. Replace (/M/G, this. getmonth ());

 

STR = Str. Replace (/DD | DD/, this. getdate ()> 9? This. getdate (). tostring (): "0" + this. getdate ());
STR = Str. Replace (/d | D/g, this. getdate ());

 

STR = Str. Replace (/HH | hh/, this. gethours ()> 9? This. gethours (). tostring (): "0" + this. gethours ());
STR = Str. Replace (/h | h/g, this. gethours ());

 

STR = Str. Replace (/MM/, this. getminutes ()> 9? This. getminutes (). tostring (): "0" + this. getminutes

());
STR = Str. Replace (/M/G, this. getminutes ());

 

STR = Str. Replace (/SS | SS/, this. getseconds ()> 9? This. getseconds (). tostring (): "0" +

This. getseconds ());
STR = Str. Replace (/S | S/g, this. getseconds ());

 

Return STR;
}

 

/*
========================================================== ===
// Date object of the instance directly by string
========================================================== ===
*/
Date. Prototype. instancefromstring = function (STR)
{
Return new date ("2004-10-10". Replace (/-/g ,"\/"));
}

 

/*
========================================================== ===
// Obtain the date following the date, year, month, and date plus a number
========================================================== ===
*/
Date. Prototype. dateadd = function (interval, number)
{
VaR date = this;

 

Switch (interval)
{
Case "Y ":
Date. setfullyear (date. getfullyear () + number );
Return date;

 

Case "Q ":
Date. setmonth (date. getmonth () + number * 3 );
Return date;

 

Case "M ":
Date. setmonth (date. getmonth () + number );
Return date;

 

Case "W ":
Date. setdate (date. getdate () + number * 7 );
Return date;

Case "D ":
Date. setdate (date. getdate () + number );
Return date;

 

Case "H ":
Date. sethours (date. gethours () + number );
Return date;

 

Case "M ":
Date. setminutes (date. getminutes () + number );
Return date;

 

Case "S ":
Date. setseconds (date. getseconds () + number );
Return date;

 

Default:
Date. setdate (D. getdate () + number );
Return date;
}
}

 

/*
========================================================== ===
// Calculate the date, year, month, and day of the two dates
========================================================== ===
*/
Date. Prototype. datediff = function (interval, objdate)
{
// Missing
}

 

/*
**************************************** **
Number function expansion
**************************************** **
*/

 

/*
========================================================== ===
// Convert to an uppercase Chinese number
========================================================== ===
*/
Number. Prototype. tochinese = function ()
{
VaR num = this;
If (! /^ \ D * (\. \ D *)? $/. Test (Num) {alert ("number is wrong! "); Return" number is wrong! ";}

 

VaR AA = new array ("zero", "one", "two", "three", "Si", "Wu", "Lu", "Lu ", "success", "fail ");
VaR BB = new array ("", "Pick", "success", "point ","");

VaR A = ("" + num ). replace (/(^ 0 *)/g ,""). split (". "), k = 0, Re = "";

 

For (VAR I = A [0]. Length-1; I> = 0; I --)
{
Switch (k)
{
Case 0: Re = BB [7] + RE; break;
Case 4: If (! New Regexp ("0 {4} \ D {" + (A [0]. length-i-1) + "} $"). Test (A [0])
Re = BB [4] + RE; break;
Case 8: Re = BB [5] + RE; bb [7] = BB [5]; k = 0; break;
}
If (K % 4 = 2 & A [0]. charat (I + 2 )! = 0 & A [0]. charat (I + 1) = 0) Re = AA [0] + RE;
If (A [0]. charat (I )! = 0) Re = AA [A [0]. charat (I)] + BB [K % 4] + RE; k ++;
}

 

If (A. length> 1) // Add the fractional part (if any)
{
Re + = BB [6];
For (VAR I = 0; I <A [1]. length; I ++) RE + = AA [A [1]. charat (I)];
}
Return re;

 

}

 

/*
========================================================== ===
// Retain the number of decimal places
========================================================== ===
*/
Number. Prototype. tofixed = function (LEN)
{

 

If (isnan (LEN) | Len = NULL)
{
Len = 0;
}
Else
{
If (LEN <0)
{
Len = 0;
}
}

 

Return math. Round (this * Math. Pow (10, Len)/Math. Pow (10, Len );

 

}

 

/*
========================================================== ===
// Convert to a large write amount
========================================================== ===
*/
Number. Prototype. tomoney = function ()
{
// Constants:
VaR maximum_number = 99999999999.99;
// Predefine the radix characters and currency symbols for output:
VaR cn_zero = "zero ";
VaR cn_one = "1 ";
VaR cn_two = "II ";
VaR cn_three = "";
VaR cn_four = "Si ";
VaR cn_five = "Wu ";
VaR cn_six = "";
VaR cn_seven = "success ";
VaR cn_eight = "success ";
VaR cn_nine = "canonical ";
VaR cn_ten = "pick up ";
VaR cn_hundred = "success ";
VaR cn_thousand = "Hangzhou ";
VaR cn_ten_thousand = "Ten Thousand ";
VaR cn_hundred_million = "";
VaR cn_symbol = "";
VaR cn_dollar = "RMB ";
VaR cn_ten_cent = "";
VaR cn_cent = "points ";
VaR cn_integer = "whole ";

// Variables:
VaR integral; // represent integral part of digit number.
VaR decimal; // represent decimal part of digit number.
VaR outputcharacters; // The output result.
VaR parts;
VaR digits, radices, bigradices, Decimals;
VaR zerocount;
VaR I, P, D;
VaR quotient, modulus;

If (this> maximum_number)
{
Return "";
}

// Process the coversion from currency digits to characters:
// Separate integral and decimal parts before processing coversion:

 

Parts = (This + ""). Split (".");
If (parts. length> 1)
{
Integral = parts [0];
Decimal = parts [1];
// Cut down redundant decimal digits that are after the second.
Decimal = decimal. substr (0, 2 );
}
Else
{
Integral = parts [0];
Decimal = "";
}
// Prepare the characters corresponding to the digits:
Digits = new array (cn_zero, cn_one, cn_two, cn_three, cn_four, cn_five, cn_six, cn_seven,

Cn_eight, cn_nine );
Radices = new array ("", cn_ten, cn_hundred, cn_thousand );
Bigradices = new array ("", cn_ten_thousand, cn_hundred_million );
Decimals = new array (cn_ten_cent, cn_cent );

 

// Start processing:
Outputcharacters = "";
// Process integral part if it is larger than 0:
If (number (Integral)> 0)
{
Zerocount = 0;
For (I = 0; I <integral. length; I ++)
{
P = integral. Length-I-1;
D = integral. substr (I, 1 );
Quotient = P/4;
Modulus = P % 4;
If (D = "0 ")
{
Zerocount ++;
}
Else
{
If (zerocount> 0)
{
Outputcharacters + = digits [0];
}
Zerocount = 0;
Outputcharacters + = digits [number (d)] + radices [modulus];
}

 

If (modulus = 0 & zerocount <4)
{
Outputcharacters + = bigradices [quotient];
}
}

 

Outputcharacters + = cn_dollar;
}

 

// Process decimal part if there is:
If (decimal! = "")
{
For (I = 0; I <decimal. length; I ++)
{
D = decimal. substr (I, 1 );
If (D! = "0 ")
{
Outputcharacters + = digits [number (d)] + decimals [I];
}
}
}

 

// Confirm and return the final output string:
If (outputcharacters = "")
{
Outputcharacters = cn_zero + cn_dollar;
}
If (decimal = "")
{
Outputcharacters + = cn_integer;
}

 

Outputcharacters = cn_symbol + outputcharacters;
Return outputcharacters;
}

 

Number. prototype. toimage = function ()
{< br> var num = array (
"# define t_width 3 \ n # define t_height 5 \ nstatic char t_bits [] = {0xf, 0x5, 0x5, 0x5, 0xf }",
"# define t_width 3 \ n # define t_height 5 \ nstatic char t_bits [] = {0x4, 0x4, 0x4, 0x4 }",
"# define t_width 3 \ n # define t_height 5 \ nstatic char t_bits [] = {0xf, 0x4, 0xf, 0x1, 0xf }",
"# define t_width 3 \ n # define t_height 5 \ nstatic char t_bits [] = {0xf, 0x4, 0xf, 0x4, 0xf }",
"# define t_width 3 \ n # define t_height 5 \ nstatic char t_bits [] = {0x5, 0x5, 0xf, 0x4, 0x4 }",
"# define t_width 3 \ n # define t_height 5 \ nstatic char t_bits [] = {0xf, 0x1, 0xf, 0x4, 0xf }",
"# define t_width 3 \ n # define t_height 5 \ nstatic char t_bits [] = {0xf, 0x1, 0xf, 0x5, 0xf }",
"# define t_width 3 \ n # define t_height 5 \ nstatic char t_bits [] = {0xf, 0x4, 0x4, 0x4, 0x4 }",
"# define t_width 3 \ n # define t_height 5 \ nstatic char t_bits [] = {0xf, 0x5, 0xf, 0x5, 0xf }",
"# define t_width 3 \ n # define t_height 5 \ nstatic char t_bits [] = {0xf, 0x5, 0xf, 0x4, 0xf} "
);

 

VaR STR = This + "";
VaR iindex
VaR result = ""
For (iindex = 0; iindex <Str. length; iindex ++)
{
Result + = "
}

 

Return result;
}

 

/*
**************************************** **
Other function extensions
**************************************** **
*/

 

/*
========================================================== ===
// Verify the class function
========================================================== ===
*/
Function isempty (OBJ)
{

 

OBJ = Document. getelementsbyname (OBJ). Item (0 );
If (TRIM (obj. Value) = "")
{

If (obj. Disabled = false & obj. readonly = false)
{
OBJ. Focus ();
}
Return true;
}
Else
{
Return false;
}
}

 

/*
========================================================== ===
// Dialog box with no mode prompt
========================================================== ===
*/
Function modelessalert (MSG)
{
Window. showmodelessdialog ("javascript: Alert (\" "+ escape (MSG) +" \ "); window. Close

(); "," "," Status: No; resizable: No; help: No; dialogheight: Height: 30px; dialogheight: 40px ;");
}

 

 

/*
========================================================== ===
// Enter the focus of the next control in the page
========================================================== ===
*/
Function enter2tab ()
{
VaR E = Document. activeelement;
If (E. tagname = "input "&&
(
E. type = "text" |
E. type = "password" |
E. type = "checkbox" |
E. type = "radio"
) |
E. tagname = "select ")

 

{
If (window. event. keycode = 13)
{
Window. event. keycode = 9;
}
}
}
/////// Enable this function. Please cancel the downstream comments.
// Document. onkeydown = enter2tab;

 

Function viewsource (URL)
{
Window. Location = 'view-Source: '+ URL;
}

 

//// Disable right-click
Document. oncontextmenu = function () {return false ;}

 

 

/*
**************************************** **
String function expansion
**************************************** **
*/

 

/*
========================================================== ===
// Remove spaces on the left
========================================================== ===

 

*/
String. Prototype. ltrim = function ()
{
Return this. Replace (/(^ \ s *)/g ,"");
}

 

String. Prototype. Mid = function (START, Len)
{
If (isnan (start) & start <0)
{
Return "";
}

 

If (isnan (LEN) & Len <0)
{
Return "";
}

 

Return this. substring (START, Len );
}

 

/*
========================================================== ===
// Remove spaces on the right
========================================================== ===
*/
String. Prototype. rtrim = function ()
{
Return this. Replace (/(\ s * $)/g ,"");
}

 

 

/*
========================================================== ===
// Remove leading and trailing Spaces
========================================================== ===
*/
String. Prototype. Trim = function ()
{
Return this. Replace (/(^ \ s *) | (\ s * $)/g ,"");
}

 

/*
========================================================== ===
// Obtain the string on the left.
========================================================== ===
*/
String. Prototype. Left = function (LEN)
{

 

If (isnan (LEN) | Len = NULL)
{
Len = This. length;
}
Else
{
If (parseint (LEN) <0 | parseint (LEN)> This. length)
{
Len = This. length;
}
}

 

Return this. substring (0, Len );
}

 

/*
========================================================== ===
// Obtain the string on the right
========================================================== ===
*/
String. Prototype. Right = function (LEN)
{

 

If (isnan (LEN) | Len = NULL)
{
Len = This. length;
}
Else
{
If (parseint (LEN) <0 | parseint (LEN)> This. length)
{
Len = This. length;
}
}

 

Return this. substring (this. Length-Len, this. Length );
}

 

/*
========================================================== ===
// Obtain the intermediate string. Note that the value starts from 0.
========================================================== ===
*/
String. Prototype. Mid = function (START, Len)
{
If (isnan (start) | start = NULL)
{
Start = 0;
}
Else
{
If (parseint (start) <0)
{
Start = 0;
}
}

 

If (isnan (LEN) | Len = NULL)
{
Len = This. length;
}
Else
{
If (parseint (LEN) <0)
{
Len = This. length;
}
}

 

Return this. substring (start, start + Len );
}

 

/*
========================================================== ===
// Search for another string in the string: The position starts from 0.
========================================================== ===
*/
String. Prototype. instr = function (STR)
{

 

If (STR = NULL)
{
STR = "";
}

 

Return this. indexof (STR );
}

 

/*
========================================================== ===
// Reverse searches for another string in the string: Starting from 0
========================================================== ===
*/
String. Prototype. Limit REV = function (STR)
{

 

If (STR = NULL)
{
STR = "";
}

 

Return this. lastindexof (STR );
}

 

 

/*
========================================================== ===
// Calculate the print length of a string
========================================================== ===
*/
String. Prototype. lengthw = function ()
{
Return this. Replace (/[^ \ x00-\ xFF]/g, "**"). length;
}

 

/*
========================================================== ===
// Whether the IP address is correct
========================================================== ===
*/
String. Prototype. isip = function ()
{

 

VaR respacecheck =/^ (\ D +) \. (\ D +) $ /;

 

If (respacecheck. Test (this ))
{
This. Match (respacecheck );
If (Regexp. $1 <=255 & Regexp. $1> = 0
& Regexp. $2 <= 255 & Regexp. $2> = 0
& Regexp. $3 <= 255 & Regexp. $3> = 0
& Regexp. $4 <= 255 & Regexp. $4> = 0)
{
Return true;
}
Else
{
Return false;
}
}
Else
{
Return false;
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.