1. JavaScript ดั้งเดิมใช้การสกัดกั้นความยาวของสตริง
การคัดลอกรหัสมีดังนี้:
ฟังก์ชั่น cutstr (str, len) {
var temp;
var icount = 0;
var patrn =/[^/x00-/xff]/;
var strre = "";
สำหรับ (var i = 0; i <str.length; i ++) {
if (icount <len - 1) {
temp = str.substr (i, 1);
if (patrn.exec (temp) == null) {
ไอคอน = ไอคอน + 1
} อื่น {
ไอคอน = ไอคอน + 2
-
strre += อุณหภูมิ
} อื่น {
หยุดพัก
-
-
return strre + "... "
-
2. JavaScript ดั้งเดิมเพื่อรับโฮสต์ชื่อโดเมน
การคัดลอกรหัสมีดังนี้:
ฟังก์ชั่น gethost (url) {
var host = "null";
if (typeof url == "undefined" || null == url) {
url = window.location.href;
-
var regex = /^/w+/://///( [idy^//like*).*/;
var match = url.match (regex);
if (typeof match! = "undefined" && null! = match) {
host = match [1];
-
โฮสต์กลับ;
-
3. JavaScript พื้นเมืองล้างช่องว่าง
การคัดลอกรหัสมีดังนี้:
string.prototype.trim = function () {
var reextraspace = /^/S*(.*?)/S+$/;
ส่งคืนสิ่งนี้ (reeptraspace, "$ 1")
-
4. JavaScript ดั้งเดิมแทนที่ทั้งหมด
การคัดลอกรหัสมีดังนี้:
string.prototype.replaceall = function (s1, s2) {
ส่งคืนสิ่งนี้ (ใหม่ regexp (S1, "GM"), S2)
-
5. JavaScript ดั้งเดิมหลบหนีแท็ก HTML
การคัดลอกรหัสมีดังนี้:
ฟังก์ชั่น htmlencode (ข้อความ) {
return text.replace (/&/g, '&'). แทนที่ (// "/g, ''). reflace (/</g, '<') แทนที่ (// g, '>')
-
6. JavaScript ดั้งเดิมคืนค่าแท็ก HTML
การคัดลอกรหัสมีดังนี้:
ฟังก์ชั่น htmldecode (ข้อความ) {
ส่งคืน text.replace (/&/g, '&'). แทนที่ (// g, '/"').replace(/</g,' <') แทนที่ (// g,'> ')
-
7. แปลงรูปแบบเวลาและวันที่ของ JavaScript
การคัดลอกรหัสมีดังนี้:
date.prototype.format = function (formatstr) {
var str = formatstr;
var Week = ['Day', 'One', 'Two', 'Three', 'Four', 'Five', 'Six'];
str = str.replace (/yyyy | yyyy/, this.getlyear ());
str = str.replace (/yy | yy/, (this.geteyear () % 100)> 9? (this.geteyear () % 100) .tostring (): '0' +
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 ());
กลับ Str
-
8. JavaScript ดั้งเดิมกำหนดว่าเป็นประเภทตัวเลข
การคัดลอกรหัสมีดังนี้:
ฟังก์ชั่น iSdigit (ค่า) {
var patrn = /^[0-9]*$ /;
if (patrn.exec (value) == null || value == "") {
กลับเท็จ
} อื่น {
กลับมาจริง
-
-
9. ตั้งค่าคุกกี้ใน JavaScript ดั้งเดิม
การคัดลอกรหัสมีดังนี้:
ฟังก์ชั่น setCookie (ชื่อ, ค่า, ชั่วโมง) {
var d = วันที่ใหม่ ();
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 + "=" + escape (value) + "; path =/; expires =" + exp.togmtstring () + "; domain = 360doc.com;"
-
10. JavaScript ดั้งเดิมได้รับค่าคุกกี้
การคัดลอกรหัสมีดังนี้:
ฟังก์ชั่น getCookie (ชื่อ) {
var arr = document.cookie.match (ใหม่ regexp ("(^|)" + ชื่อ + "= ([^;]*) (; | $)"));
ถ้า (arr! = null) ส่งคืน Unsescape (arr [2]);
คืนค่า NULL
-