1, native JavaScript implementation string length intercept
Copy Code code as follows:
function Cutstr (str, len) {
var temp;
var icount = 0;
var patrn =/[^\x00-\xff]/;
var Strre = "";
for (var i = 0; i < str.length; i++) {
if (Icount < len-1) {
temp = Str.substr (i, 1);
if (patrn.exec (temp) = null) {
icount = icount + 1
} else {
icount = icount + 2
}
Strre + Temp
} else {
Break
}
}
return Strre + "..."
}
2, native JavaScript acquisition domain name host
Copy Code code as follows:
function GetHost (URL) {
var host = "NULL";
if (typeof url = = "undefined" | | | null = = URL) {
url = window.location.href;
}
var regex =/^\w+\:\/\/([^\/]*). * *;
var match = Url.match (regex);
if (typeof match!= "undefined" && null!= match) {
host = match[1];
}
return host;
}
3, native JavaScript clear space
Copy Code code as follows:
String.prototype.trim = function () {
var reextraspace =/^\s* (. *?) \s+$/;
Return This.replace (Reextraspace, "$")
}
4. Native JavaScript replaces all
Copy Code code as follows:
String.prototype.replaceAll = function (S1, S2) {
Return This.replace (New RegExp (S1, "GM"), S2)
}
5. Native JavaScript Escape HTML tags
Copy Code code as follows:
function HtmlEncode (text) {
Return Text.replace (/&/g, ' & '). Replace (/\ "/g, '"). Replace (/</g, ' < '). Replace (/>/g, ' > ')
}
6. Native JavaScript Restore HTML tags
Copy Code code as follows:
function HtmlDecode (text) {
Return Text.replace (/&/g, ' & '). Replace (/"/g, ' \"). Replace (/</g, ' < '). Replace (/>/g, ' > ')
}
7. Native JavaScript time date format conversion
Copy Code code as follows:
Date.prototype.Format = function (formatstr) {
var str = FORMATSTR;
var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six '];
str = str.replace (/yyyy| yyyy/, This.getfullyear ());
str = str.replace (/yy| yy/, (this.getyear ()%) > 9? (This.getyear ()%). ToString (): ' 0 ' + (this.getyear ()% 100));
str = str.replace (/mm/, This.getmonth () + 1) > 9? (This.getmonth () + 1). ToString (): ' 0 ' + (this.getmonth () + 1));
str = str.replace (/m/g, (This.getmonth () + 1));
str = str.replace (/w| W/g, Week[this.getday ()]);
str = str.replace (/dd| Dd/, This.getdate () > 9? This.getdate (). toString (): ' 0 ' + this.getdate ());
str = str.replace (/d| D/g, This.getdate ());
str = str.replace (/hh| hh/, This.gethours () > 9? This.gethours (). toString (): ' 0 ' + this.gethours ());
str = str.replace (/h| H/g, This.gethours ());
str = str.replace (/mm/, This.getminutes () > 9 this.getminutes (). toString (): ' 0 ' + this.getminutes ());
str = str.replace (/m/g, This.getminutes ());
str = str.replace (/ss| ss/, This.getseconds () > 9? This.getseconds (). toString (): ' 0 ' + this.getseconds ());
str = str.replace (/s| S/g, This.getseconds ());
Return str
}
8. Native JavaScript Determines whether it is a numeric type
Copy Code code as follows:
function IsDigit (value) {
var patrn =/^[0-9]*$/;
if (patrn.exec (value) = NULL | | value = = "") {
return False
} else {
return True
}
}
9, native JavaScript set cookie value
Copy Code code as follows:
function Setcookie (name, value, Hours) {
var d = new Date ();
var offset = 8;
var UTC = D.gettime () + (D.gettimezoneoffset () * 60000);
var nd = UTC + (3600000 * offset);
var exp = new Date (ND);
Exp.settime (Exp.gettime () + Hours * 60 * 60 * 1000);
Document.cookie = name + "=" + Escape (value) + ";p ath=/;expires=" + exp.togmtstring () + ";d omain=360doc.com;"
}
10. Native JavaScript gets cookie value
Copy Code code as follows:
function GetCookie (name) {
var arr = Document.cookie.match (New RegExp ("(^|)" + name + "= ([^;] *)(;|$)"));
if (arr!= null) return unescape (arr[2));
return null
}