1. Нативный JavaScript реализует перехват длины строки
Кода -копия выглядит следующим образом:
Функция 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) {
icon = icon + 1
} еще {
icon = icon + 2
}
Strre += Temp
} еще {
перерыв
}
}
вернуть Strre + "..."
}
2. Native JavaScript, чтобы получить хост доменного имени
Кода -копия выглядит следующим образом:
Функция gethost (url) {
var host = "null";
if (typeof url == "undefined" || null == url) {
url = window.location.href;
}
var regex = /^/w+/:////(nmb^//^*).*//;
var match = url.match (regex);
if (typeof match! = "undefined" && null! = match) {
host = match [1];
}
возвратный хост;
}
3. Native JavaScript очищает места
Кода -копия выглядит следующим образом:
String.prototype.trim = function () {
var reextraspace = /^/s*(.*?)/s+$/;
вернуть это. Пересечение (Reextraspace, "$ 1")
}
4. Native JavaScript замените все
Кода -копия выглядит следующим образом:
String.prototype.replaceall = function (s1, s2) {
вернуть это. Перейдите (New Regexp (S1, "GM"), S2)
}
5. Native JavaScript выходит из HTML -тегов
Кода -копия выглядит следующим образом:
функция htmlencode (text) {
return Text.Replace (/&/g, '&'). Заменить (// "/g, '') .replace (/</g, '<' '). Заменить (// g,'> ')
}
6. Native JavaScript восстанавливает HTML -теги
Кода -копия выглядит следующим образом:
функция htmldecode (text) {
return Text.Replace (/&/g, '&'). Заменить (// g, '/"').replace(/</g,' <'). Заменить (// g,'> ')
}
7. Преобразовать нативные временные и дату формата
Кода -копия выглядит следующим образом:
Date.prototype.format = function (formatstr) {
var str = formatstr;
var week = ['day', 'one', 'two', 'three', 'four', 'five', 'six'];
str = str.Replace (/yyyy | yyyy/, this.getbleear ());
str = str.replace (/yy | yy/, (this.getyear () % 100)> 9? (this.getyear () % 100) .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, неделя [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 ());
возврат стр
}
8. Native JavaScript определяет, является ли это числовым типом
Кода -копия выглядит следующим образом:
функция isDigit (значение) {
var patrn = /^[0-9]*$ /;
if (patrn.exec (value) == null || value == "") {
вернуть ложь
} еще {
вернуть правду
}
}
9. Установите значение cookie в нативном JavaScript
Кода -копия выглядит следующим образом:
Функция setCookie (имя, значение, часы) {
var d = new Date ();
var offset = 8;
var utc = d.gettime () + (d.gettimezoneoffset () * 60000);
var nd = utc + (3600000 * смещение);
var exp = новая дата (ND);
exp.settime (exp.gettime () + часы * 60 * 60 * 1000);
document.cookie = name + "=" + exck (value) + "; path =/; истекает =" + exp.togmtString () + "; domain = 360doc.com;"
}
10. Native JavaScript получает ценность cookie
Кода -копия выглядит следующим образом:
функция getCookie (имя) {
var arr = document.cookie.match (new Regexp ("(^|)" + name + "= ([^;]*) (; | $)"));
if (arr! = null) вернуть Unessape (arr [2]);
вернуть ноль
}