For now everyone in peacetime development, will write a number of Repetitive JS processing code, today summed up several more commonly used methods to achieve. Gets the GET request parameter, going to the string space.
1. Getting the parameters in GET request
JS Code
function Getpara (para) {
if (location.href.indexOf ("?") = = 1) {
//without arguments, then do nothing.
return null;
}
else{
//Get request string
var urlquery = Location.href.split ("?").
if (Urlquery[1].indexof ("&") ==-1) {///Only one parameter
if (urlquery[1].indexof ("=") = = 1) {
//no equals sign, no arguments, then do Nothing return
null;
} else{
var keyvalue = urlquery[1].split ("=");
var key = keyvalue[0];
var value = keyvalue[1];
if (Key==para) {return
value;
}
}
} else{
//parse parameter
var urlterms = Urlquery[1].split ("&");
for (var i = 0; I <urlterms.length;i++) {
var keyvalue = urlterms[i].split ("=");
var key = keyvalue[0];
var value = keyvalue[1];
if (Key==para) {return
value;
}
}}} return null;
}
2.//This function is used to remove the space left by the string
JS Code
function Lefttrim (str) {
if (str.charat (0) = = "") {
str = str.slice (1);
str = Lefttrim (str);
}
return str;
}
3.//This function is used to remove the space to the right of the string
JS Code
function Righttrim (str) {
if (str.length-1 >= 0 && str.charat (str.length-1) = "") {
str = STR.SL Ice (0, str.length-1);
str = Righttrim (str);
}
return str;
}
4.//Convert time to fixed format output
JS Code
/** * Converts time to a fixed format output * New Date () Toformat (' Yyyy-mm-dd HH:mm:ss ');
* New Date (). Toformat (' Yyyy/mm/dd hh:mm:ss '); * Only support keywords (yyyy, MM, DD, hh, hh, MM, ss) HH: For 24 hours, HH said 12 hours/date.prototype.toformatstring=function (format) {var formatstr
= format; if (format!= null && format!= "") {//Set year if (Formatstr.indexof ("yyyy") >=0) {formatstr = f
Ormatstr.replace ("yyyy", this.getfullyear ());
}//Set the month if (Formatstr.indexof ("MM") >=0) {var month = This.getmonth () + 1;
if (Month <) {month = "0" + month;
} FORMATSTR = Formatstr.replace ("MM", month);
//Set Daily if (Formatstr.indexof ("DD") >=0) {var day = This.getday ();
if (Day < ten) {day = ' 0 ' + day;
} FORMATSTR = Formatstr.replace ("dd", day);
}//Set-24 hours var hours = this.gethours (); if (Formatstr.indexof ("HH") >=0) {if (month <) {month ="0" + month;
} FORMATSTR = Formatstr.replace ("HH", hours);
}//Set-12 hours if (Formatstr.indexof ("hh") >=0) {if (Hours >) {hours = hours-12;
} if (Hours <) {hours = "0" + hours;
} FORMATSTR = Formatstr.replace ("hh", hours);
//Set the split if (Formatstr.indexof ("mm") >=0) {var minute = this.getminutes ();
if (minute <) {minute = "0" + minute;
} FORMATSTR = Formatstr.replace ("mm", minute);
}//Set the second if (Formatstr.indexof ("ss") >=0) {var second = this.getseconds ();
if (second <) {second = "0" + second;
} FORMATSTR = Formatstr.replace ("ss", second);
} return FORMATSTR; }
The above is the entire contents of this article, I hope you can enjoy.