1. Native JavaScript implementa la intercepción de longitud de cadena
La copia del código es la siguiente:
función CUTSTR (STR, LEN) {
var temp;
var icount = 0;
var Patrn =/[^/x00-/xff]/;
var strre = "";
para (var i = 0; i <str.length; i ++) {
if (icount <len - 1) {
temp = str.substr (i, 1);
if (patrn.exec (temp) == null) {
icono = icono + 1
} demás {
icono = icono + 2
}
strre += temp
} demás {
romper
}
}
Regrese Strre + "..."
}
2. JavaScript nativo para obtener el host de nombre de dominio
La copia del código es la siguiente:
función gethost (url) {
var host = "null";
if (typeof url == "indefinido" || null == url) {
url = window.location.href;
}
var regex = /^/w+/:///([^//font>).*/;
var match = url.match (regex);
if (typeof Match! = "Undefined" && null! = Match) {
host = Match [1];
}
Host de regreso;
}
3. JavaScript nativo borra espacios
La copia del código es la siguiente:
String.prototype.trim = function () {
var reextraSpace = /^/s*(.*?)/s+$/;
Devuelve this.replace (ReextraSpace, "$ 1")
}
4. JavaScript nativo reemplazar todo
La copia del código es la siguiente:
String.prototype.replaceall = function (s1, s2) {
Devuelve this.replace (nuevo Regexp (S1, "GM"), S2)
}
5. JavaScript nativo escapa de etiquetas HTML
La copia del código es la siguiente:
función htmlencode (text) {
return text.replace (/&/g, '&'). reemplazar (// "/g, '') .replace (/</g, '<'). Reemplazar (// g, '>')
}
6. JavaScript nativo restaura etiquetas HTML
La copia del código es la siguiente:
función htmldecode (text) {
return text.replace (/&/g, '&'). Reemplazar (// g, '/"').replace(/</g,' <'). Reemplazar (// g,'> ')
}
7. Convierta el formato de hora y fecha de JavaScript nativo
La copia del código es la siguiente:
Date.prototype.format = function (formatstr) {
var str = formatstr;
var Week = ['Day', 'One', 'Two', 'Three', 'Four', 'Five', 'Six'];
str = str.replace (/yyyy | yyyy/, this.getblyar ());
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, semana [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 ());
devolver str
}
8. JavaScript nativo determina si es un tipo numérico
La copia del código es la siguiente:
función isDigit (valor) {
var Patrn = /^[0-9]*$ /;
if (patrn.exec (valor) == null || valor == "") {
devolver falso
} demás {
Devolver verdadero
}
}
9. Establezca el valor de las cookies en JavaScript nativo
La copia del código es la siguiente:
función setcookie (nombre, valor, horas) {
var d = nueva fecha ();
Var offset = 8;
var utc = d.gettime () + (d.gettimezoneOffset () * 60000);
var nd = utc + (3600000 * offset);
var exp = nueva fecha (nd);
exp.setTime (exp.gettime () + horas * 60 * 60 * 1000);
document.cookie = name + "=" + escape (valor) + "; path =/; expires =" + exp.togmtstring () + "; dominio = 360doc.com;"
}
10. JavaScript nativo obtiene el valor de las cookies
La copia del código es la siguiente:
function getCookie (nombre) {
var arr = document.cookie.match (new Regexp ("(^|)" + nombre + "= ([^;]*) (; | $)");
if (arr! = null) return unescape (arr [2]);
regresa nulo
}