1. JavaScript natif implémente l'interception de longueur de chaîne
La copie de code est la suivante:
fonction Cutstr (str, len) {
Var Temp;
var iCount = 0;
var patn = / [^ / x00- / xff] /;
var strre = "";
pour (var i = 0; i <str.length; i ++) {
if (iCount <len - 1) {
temp = str.substr (i, 1);
if (patn.exec (temp) == null) {
icône = icône + 1
} autre {
icône = icône + 2
}
strre + = temp
} autre {
casser
}
}
Retour Strre + "..."
}
2. JavaScript natif pour obtenir l'hôte de nom de domaine
La copie de code est la suivante:
fonction gethost (url) {
var host = "null";
if (typeof url == "Undefined" || null == url) {
url = window.location.href;
}
var regex = /^/w+/:////(@^// / Lodal*).*/;
var match = url.match (regex);
if (typeof correspond! = "Undefined" && null! = correspond) {
host = correspond [1];
}
retour hôte;
}
3. JavaScript natif efface les espaces
La copie de code est la suivante:
String.prototype.trim = function () {
var reextraspace = /^/s*(.*?)/S+$/;
Renvoyez ceci.replace (Reextraspace, "1 $")
}
4. JavaScript natif remplace tout
La copie de code est la suivante:
String.prototype.replaceAll = fonction (s1, s2) {
Renvoie ceci.replace (New Regexp (S1, "GM"), S2)
}
5. JavaScript natif échappe aux balises HTML
La copie de code est la suivante:
fonction htmlencode (text) {
return text.replace (/ & / g, '&'). remplacer (// "/ g, '') .replace (/ </ g, '<'). Remplacer (// g, '>')
}
6. JavaScript natif restaure les balises HTML
La copie de code est la suivante:
fonction htmlDECODE (texte) {
return text.replace (/ & / g, '&'). Remplacer (// g, '/"').replace(/</g,' <'). Remplacer (// g,'> ')
}
7. Convertir le format d'heure et de date de JavaScript natif
La copie de code est la suivante:
Date.prototype.format = fonction (formatstr) {
var str = formattr;
var semaine = [«jour», «un», «deux», «trois», «quatre», «cinq», «six»];
str = str.replace (/ yyyy | yyyy /, this.getlyear ());
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, semaine [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 ());
Retour Str
}
8. JavaScript natif détermine s'il s'agit d'un type numérique
La copie de code est la suivante:
fonction isdigit (valeur) {
var patn = / ^ [0-9] * $ /;
if (patn.exec (valeur) == null || value == "") {
retourner faux
} autre {
Retour vrai
}
}
9. Définir la valeur des cookies en JavaScript natif
La copie de code est la suivante:
fonction setcookie (nom, valeur, heures) {
var d = new Date ();
var offset = 8;
var utc = d.gettime () + (d.getTimeZoneOffset () * 60000);
var nd = utc + (3600000 * offset);
var exp = nouvelle date (nd);
exp.setTime (exp.gettime () + heures * 60 * 60 * 1000);
document.cookie = name + "=" + Escape (valeur) + "; path = /; exires =" + exp.togmtsstring () + "; domain = 360doc.com;"
}
10. JavaScript natif obtient une valeur de cookie
La copie de code est la suivante:
fonction getcookie (name) {
var arr = document.cookie.match (new regexp ("(^ |)" + name + "= ([^;] *) (; | $)"));
if (arr! = null) return Unescape (arr [2]);
retourner null
}