Sort out common JS Functions

Source: Internet
Author: User

Js get page address parameters
Copy codeThe Code is as follows:
Function getUrlPara (paraName)
{
Var sUrl = location. href;
Var sReg = "(? ://? | &) {1} "+ paraName +" = ([^ &] *)"
Var re = new RegExp (sReg, "gi ");
Re.exe c (sUrl );
Return RegExp. $1;
}

Address jump
Copy codeThe Code is as follows:
Var pn = $ ("# gotopagenum"). val (); // # gotopagenum is the id attribute of the text box.
Location. href = "NewList. aspx? Pagenum = "+ pn; // location. href: Jump to the client page

Sub-location
Copy codeThe Code is as follows:
Function Convert (money)
{
Var s = money; // obtain decimal data
S + = "";
If (s. indexOf (".") =-1) s + = ". 00"; // if there is no decimal point, add the following decimal points and 00
If (/\. \ d $/. test (s) s + = "0"; // Regular Expression
While (/\ d {4} (\. |,)/. test (s) // replace
S = s. replace (/(\ d) (\ d {3 }(\. |,)/, "$1, $2"); // Add
Return s;
}

Determines whether a number is used.
Copy codeThe Code is as follows:
Function IsNumeric (txt ){
If (txt = ""){
Return false;
}

If (txt. indexOf (",")> 0 ){
Txt = txt. replace (",","");
}

If (isNaN (txt )){
Return false;
}
Else {
Return true;
}
}

Format the number with two decimal places
Copy codeThe Code is as follows:
Function changeTwoDecimal_f (x ){
Var f_x = parseFloat (x );
If (isNaN (f_x )){
Alert ('function: changeTwoDecimal-> parameter error ');
Return false;
}
F_x = Math. round (f_x * 100)/100;
Var s_x = f_x.toString ();
Var pos_decimal = s_x.indexOf ('.');
If (pos_decimal <0 ){
Pos_decimal = s_x.length;
S_x + = '.';
}
While (s_x.length <= pos_decimal + 2 ){
S_x + = '0 ';
}
Return s_x;
}

The number calculation function parseFloat parseInt in Js.

Preset query conditions for the current js date yyyy-mm-dd
Copy codeThe Code is as follows:
Var now = new Date ();
Var year = now. getYear ();

If (now. getYear () <1900 ){
Year = now. getYear () + 1900;
}

Var month = now. getMonth () + 1;
Var day = now. getDate ();

If (month <10) month = "0" + month;
If (day <10) day = "0" + day;

$ ("# TxtDate1"). val (year. toString () + "-" + month. toString () + "-01 ");
$ ("# TxtDate2"). val (year. toString () + "-" + month. toString () + "-" + day. toString ());

Js gets the timestamp, replacing the Guid in some scenarios
Copy codeThe Code is as follows:
Function NowTimeCode ()
{
Var Result = "";

Var now = new Date ();

Var year = now. getYear ();

If (now. getYear () <1900 ){
Year = now. getYear () + 1900;
}

Var month = now. getMonth () + 1;
Var day = now. getDate ();
Var hour = now. getHours ();
Var minutes = now. getMinutes ();
Var second = now. getSeconds ();
Var millisecond = now. getMilliseconds ();

If (month <10) month = "0" + month;
If (day <10) day = "0" + day;
If (hour <10) hour = "0" + hour;
If (minutes <10) minutes = "0" + minutes;
If (second <10) second = "0" + second;

If (millisecond <10)
Millisecond = "00" + millisecond;
Else
{
If (millisecond <100)
{
Millisecond = "0" + millisecond;
}
}

Result = year. toString () + month. toString () + day. toString () + hour. toString () + minutes. toString () + second. toString () + millisecond. toString ();

Return Result;

}

Related Article

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.