Page 1/2 of some nice JS User-Defined Functions

Source: Internet
Author: User

1. dayin ()
Purpose:You can create a page and print the content with the id of dayin to solve the problem of printing part of the content on a page.
Usage:The content to be printed is included by <span id = "dayin"> </span>, and then an event is defined in a button.
<Input type = "button" onclick = "dayin ()" value = "print">
Copy codeThe Code is as follows:
Function dayin ()
{
Var code = "<body onload = window. print ()>"
Code + = document. all. dayin. innerHTML;
Code = code. toUpperCase ();
Code = code. replace (/<A [^>] *> 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 (/<A [^>] *> Delete <\/A>/gi ,"");
Is to filter out all the deleted connections in the content

2. isNumber (st)
Function: Determines whether the st variable is composed of numbers (including negative numbers and decimals). If true is returned, false is returned.
Copy codeThe Code is as follows:
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.
Copy codeThe Code is as follows:
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.
Copy codeThe Code is as follows:
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 Chinese is supported)
Copy codeThe Code is as follows:
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
Copy codeThe Code is as follows:
Function hideObject (obj ){
Obj. style. display = "none ";
}

7. showObject (obj)
Purpose: Display obj
Copy codeThe Code is as follows:
Function showObject (obj ){
Obj. style. display = "block ";
}

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.