Summarize some JS custom functions

Source: Internet
Author: User
js| function

1, Dayin ()
Role: The ID for dayin content, new page and print, you can solve the problem of printing some of the content of a page.
How to: Include content that will be printed through the <span id= "Dayin" ></span>, and then define the event in a press twist
<input type= "button" value= "Print" >

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.document.write (code);
Newwin.document.close ();
}

where Code=code.replace (/<a[^>]*> delete <\/a>/gi, "");
is to filter out all deletion connections in the content
 
2, Isnumber (ST)
Role: Determine whether the variable St consists of numbers (both negative and decimal), or False if it returns true.
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)
Function: Create a cookie that is named name, values, and is valid for days. Can be modified at the same time.
function Createcookie (name,value,days) {
var expires = "";
if (days) {
var date = new Date ();
Date.settime (Date.gettime () + (days*24*60*60*1000));
expires = "; Expires= "+date.togmtstring ();
};
Document.cookie = name+ "=" +value+expires+ "; path=/";
};

4, Readcookie (name)
Function: Reads the value of a cookie according to its name. If none, 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)
Function: Get the value of a parameter in the browser's address bar (not perfect, for example, if you have a space, you will get%20, but support 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)
Role: Hide obj
function Hideobject (obj) {
Obj.style.display = "None";
}

7, Showobject (obj)
function: Show obj
function Showobject (obj) {
Obj.style.display = "block";
}

8, Trim (str)
function: Go to str on both sides of the space

function Trim (str)
{
Return Str.replace (/^\s*|\s*$/g, "");
}

9, Function bj_date (D1,D2)
Role: Compare the size of the D1,d2 date
function Bj_date (D1,D2)
{
/*
Author:wxg
Effect: Compare Date Size
Parameters: D1 D2
Character year-month-day type, such as 2005-01-22
return value: 0/1/2
Digital type
D1>D2 return 0
D1=D2 return 1
D1<D2 return 2
*/
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 numbers into currency format
function Setcurrency (s) {
if (/[^0-9\.\-]/.test (s)) return "Invalid value";
S=s.replace (/^ (\d*) $/, "$");
S= (s+). Replace (/(\d*\.\d\d) \d*/, "$");
S=s.replace (".", ",");
var re=/(\d) (\d{3},)/;
while (Re.test (s))
S=s.replace (Re, "$1,$2");
S=s.replace (/, (\d\d) $/, ". $");
Return S.replace (/^\./, "0.")
}

11, run the code

function RunCode (obj) {
var winname = window.open ("," _blank ",");
Winname.document.open (' text/html ', ' replace ');
Winname.opener = NULL//Prevent code to talk about page modification
Winname.document.writeln (Obj.value);
Winname.document.close ();
}

12, Save the Code

function Savecode (obj) {
var winname = window.open (', ' _blank ', ' top=10000 ');
Winname.document.open (' text/html ', ' replace ');
Winname.document.writeln (Obj.value);
Winname.document.execCommand (' SaveAs ', ', ', ' code.htm ');
Winname.close ();
}

function StrLen (str) {
var count=0, ASC, test= ' Chinese ';
if (test.length==2) {
for (Var i=0;i<str.length;i++) {
ASC = Str.charcodeat (i);
if (ASC < 0) ASC = 65536;
if (ASC > 255) count++;
}
}
return str.length+count;
}
Intercepts the left string by number of bytes
function Strsubleft (str, len) {
var count=0, ASC, ret = "";
for (Var i=0;i<str.length;i++) {
ASC = Str.charcodeat (i);
if (ASC < 0) ASC = 65536;
if (ASC > 255) count++;
if (i + count >= len) {
ret = str.substr (0, I);
Break
}
}
return ret;
}
function Getrandom () {
Return "&" +math.random () *1000+ "&";
}
function Checkint (str) {
if (str==null| | str== "") {
Alert ("Please enter a number!");
return false;
}
var digits = "1234567890";
var i = 0;
var strlen = str.length;
while ((I<strlen)) {
var char = Str.charat (i);
if (Digits.indexof (char) = =-1) {
Alert ("Please enter the correct number!");
return false;
}
i++;
}
return true;
}
function Hasword (str) {
if (str.replace (//g, ' ") = =" "| | Str.replace (",") = = "") {
return false;
}else{
return true;
}
}
function ReplaceAll (OLDSTR,FINDSTR,REPSTR) {
var srchndx = 0;
var newstr = "";
while (Oldstr.indexof (FINDSTR,SRCHNDX)!=-1) {
Newstr + + oldstr.substring (Srchndx,oldstr.indexof (FINDSTR,SRCHNDX));
Newstr + = Repstr;
Srchndx = (Oldstr.indexof (findstr,srchndx) + findstr.length);
}
Newstr + + oldstr.substring (srchndx,oldstr.length);
return newstr;
}

function Checkprice (inputvalue) {
Inputvalue= "" +inputvalue;

Myregexp =/^ (\d+), (\d+) $/;
if (Inputvalue.match (MYREGEXP)) return true;
else return false;
}
/* Verify positive integer * *
function Checknum (VALUESTR) {
var myregexp =/^ (\d+) $/;
if (Valuestr.match (myregexp)) {
return true;
}else{
return false;
}
}
/* Limit the number of characters in obj (usually textarea) * *
function Checkwordslength (obj,maxlength) {
if (obj.value.length>=maxlength) {
Obj.value= "http://www.xrss.cn/obj.value.substring (0,maxlength-1);
" }
}



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.