Organize some commonly used JS functions, I hope to help you.
1, Dayin ()
function:Create a new page and print the ID dayin content to solve the problem of printing portions of a page.
How to use:The content to be printed is contained by <span id= "Dayin" ></span>, and then the event is defined in one of the buttons <input type= "button" onclick= "Dayin ()" 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 (); } |
Wherein Code=code.replace (/<a[^>]*> delete <\/a>/gi, "");
is to filter out all deletion connections in the content
2, Isnumber (ST)
function: Determines whether the variable St is composed of numbers (both negative and decimal) and 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 the cookie according to the 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 a perfect solution, 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)
function: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)
function: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 formatfunction 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 codefunction 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 Codefunction 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;
To intercept the left string by byte number
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==nullstr== "") {
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://blogbeta.blueidea.com/obj.value.substring (0,maxlength-1);
" }
}