Summary some javascript UDF _ javascript skills

Source: Internet
Author: User
Summary of some js custom functions 1. dayin ()
Purpose: Create a page and print the content with the id "dayin" to solve the problem of printing part of the content on a page.
Usage: the content to be printed is included and then defined in a button
Event


Function dayin ()
{
Var code =""
Code + = document. all. dayin. innerHTML;
Code = code. toUpperCase ();
Code = code. replace (/] *> Delete <\/A>/gi ,"");
Code = code. toLowerCase ();
Var newwin = window. open ('','','');
Newwin. opener = null;
Newwin.doc ument. write (code );
Newwin.doc ument. close ();
}


Code = code. replace (/] *> Delete <\/A>/gi ,"");
Is to filter out all the deleted connections in the content

2. isNumber (st)
Purpose: judge whether the st variable is composed of numbers (including negative numbers and decimals). If true is returned, false is returned.
Function isNumber (st)
{
Var Letters = "1234567890 -.";
Var I;
Var c;
If (st. charAt (0) = '.')
Return false;
If (st. charAt (0) = '-' & st. charAt (1) = '.')
Return false;
If (st. charAt (st. length-1) = '-')
Return false;
For (I = 0; I <st. length; I ++)
{
C = st. charAt (I );
If (Letters. indexOf (c) <0)
Return false;
}
Return true;
}


3. createCookie (name, value, days)
Purpose: Create a cookie with the name, value as values, and validity period as days. It can also be modified.
Function createCookie (name, value, days ){
Var expires = "";
If (days ){
Var date = new Date ();
Date. setTime (date. getTime () + (days * 24x60*60*1000 ));
Expires = "; expires =" + date. toGMTString ();
};
Document. cookie = name + "=" + value + expires + "; path = /";
};

4. readCookie (name)
Purpose: Read the cookie value based on the name. If no value exists, null is returned.
Function readCookie (name ){
Var nameEQ = name + "= ";
Var ca = document. cookie. split (';');
For (var I = 0; I <ca. length; I ++ ){
Var c = ca [I];
While (c. charAt (0) = '') c = c. substring (1, c. length );
If (c. indexOf (nameEQ) = 0) return c. substring (nameEQ. length, c. length );
};
Return null;
};

5. request (st)
Purpose: Get the value of a parameter in the address bar of the browser (not perfect. For example, % 20 will be obtained if there is space, but it is supported
(Chinese)
Function request (st ){
Var ustr = document. location. search;
Var intPos = ustr. indexOf ("? ");
Var strRight = ustr. substr (intPos + 1 );
Var arrTmp = strRight. split ("% 26 ");
For (var I = 0; I <arrTmp. length; I ++)
{
Var arrTemp = arrTmp [I]. split ("= ");
If (arrTemp [0]. toUpperCase () = st. toUpperCase () return arrTemp [1];
}
Return "";
}

6. hideObject (obj)
Purpose: Hide obj
Function hideObject (obj ){
Obj. style. display = "none ";
}

7. showObject (obj)
Purpose: Display obj
Function showObject (obj ){
Obj. style. display = "block ";
}

8. trim (str)
Purpose: Remove spaces on both sides of str

Function trim (str)
{
Return str. replace (/^ \ s * | \ s * $/g ,"");
}

9. function bj_date (d1, d2)
Purpose: Compare the date sizes of d1 and d2
Function bj_date (d1, d2)
{
/*
Author: wxg
Purpose: Compare the date size.
Parameter: d1 d2
Renewed year-month-day type, for example
Return Value: 0/1/2
Number Type
D1> d2 returns 0
D1 = d2 returns 1
D1 */
If (d1 = "" & d2 = ""){
Return 3
}
If (d1 = "" | d2 = ""){
Return 4
}
D1 = d1.split ("-")
D2 = d2.split ("-")
Var a = new Date (Number (d1 [0]), Number (d1 [1]), Number (d1 [2])
Var B = new Date (Number (d2 [0]), Number (d2 [1]), Number (d2 [2])
A = a. valueOf ()
B = B. valueOf ()
If (a-B> 0)
Return 0
If (a-B = 0)
Return 1
If (a-B <0)
Return 2
}


10. format the number into a currency format
Function setCurrency (s ){
If (/[^ 0-9 \. \-]/. test (s) return "invalid value ";
S = s. replace (/^ (\ d *) $/, "$1 .");
S = (s + "00"). replace (/(\ d * \. \ d) \ d */, "$1 ");
S = s. replace (".",",");
Var re =/(\ d) (\ d {3 },)/;
While (re. test (s ))
S = s. replace (re, "$1, $2 ");
S = s. replace (/, (\ d) $/, ". $1 ");
Return s. replace (/^ \./, "0 .")
}
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.