1. 封装cookie组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
var Cookie = { // 读取 read : function(name){ var cookieStr = "; "+document.cookie+"; "; var index = cookieStr.indexOf("; "+name+"="); if (index!=-1){ var s = cookieStr.substring(index+name.length+3,cookieStr.length); return unescape(s.substring(0, s.indexOf("; "))); }else{ return null; } }, // 设置 set : function(name,value,expires){ var expDays = expires*24*60*60*1000; var expDate = new Date(); expDate.setTime(expDate.getTime()+expDays); var expString = expires ? "; expires="+expDate.toGMTString() : ""; var pathString = ";path=/"; document.cookie = name + "=" + escape(value) + expString + pathString; }, // 删除 del : function(name){ var exp = new Date(new Date().getTime()-1); var s=this.read(name); if(s!=null) { document.cookie= name + "="+s+"; expires="+exp.toGMTString()+";path=/" } } }; // demo: Cookie.set("xuanfengge", "www.xuanfengge.com", 7); alert(Cookie.read("xuanfengge")); Cookie.del("xuanfengge"); |
2. 字符串比较
1 2 3 4 5 |
var aString = "Hello!"; var bString = new String("Hello!"); if( aString == "Hello!" ){ } //结果: true if( aString == bString ){ } //结果: true if( aString === bString ){ } //结果: false (两个对不同,尽管它们的值相同) |
3. 检索字符串
1 2 3 |
var myString = "hello everybody."; // 如果检索不到会返回-1,检索到的话返回在该串中的起始位置 if( myString.indexOf("every") > -1 ){ } //结果: true |
4. 查找替换字符串
1 2 |
var myString = "I is your father."; var result = myString.replace("is","am"); //结果: "I am your father." |
5. 截取字符串
1 2 3 4 5 6 7 8 9 10 11 12 |
//截取第 6 位开始的字符 var myString = "Every good boy does fine."; var section = myString.substring(6); //结果: "good boy does fine." //截取第 0 位开始至第 10 位为止的字符 var myString = "Every good boy does fine."; var section = myString.substring(0,10); //结果: "Every good" //截取从第 11 位到倒数第 6 位为止的字符 var myString = "Every good boy does fine."; var section = myString.slice(11,-6); //结果: "boy does" //从第 6 位开始截取长度为 4 的字符 var myString = "Every good boy does fine."; var section = myString.substr(6,4); //结果: "good" |
6. 字数统计(中英文)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function fucCheckLength(strTemp){ var i,sum; sum=0; for(i=0;i<strTemp.length;i++){ if ((strTemp.charCodeAt(i)>=0) && (strTemp.charCodeAt(i)<=255)){ sum=sum+1; }else{ sum=sum+2; } } return sum; } // demo: fucCheckLength("qwe"); //3 fucCheckLength("轩枫"); //4 |
7. 字符串是否合法日期
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//合法日期:yyyy-mm-dd,分隔符可以为-/. String.prototype.isDate = function() { var year,month,date,day; var reg = /^([12]\d{3})([-\/\.])([01]?\d)\2([0-3]?\d)$/; if (!reg.test(this)) return false; year = RegExp.$1.parseInt(); month = RegExp.$3.parseInt() - 1; date = RegExp.$4.parseInt(); day = new Date(year,month,date); return ((date == day.getDate()) && (month == day.getMonth())); } // demo: "1999-12-20".isDate() // 合法日期:yyyy-mm-dd,分隔符可以为-/ |
8. 返回两个日期之间的时间间隔
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
function dateDiff(date1,date2) { var year,month,date,day1,day2; var reg = /^(\d{4})([-\/\.])(\d{2})\2(\d{2})$/; reg.test(date1); year = RegExp.$1.parseInt(); month = RegExp.$3.parseInt() - 1; date = RegExp.$4.parseInt(); day1 = new Date(year,month,date); reg.test(date2); year = RegExp.$1.parseInt(); month = RegExp.$3.parseInt() - 1; date = RegExp.$4.parseInt(); day2 = new Date(year,month,date); return (day1.getTime() - day2.getTime()) / 86400000; } // demo: // 用以前,先用isDate()判断一下是否合法日期 // 注意:这个函数的reg和isDate()里的reg小you不同 // 以天为单位(date1-date2=?),可以用来比较2个日期大小 |
9. 判断是否为空对象
1 2 3 4 5 6 7 8 9 10 11 12 |
function isEmpty(obj){ for(var key in obj){ return false; } return true; } // demo: isEmpty({}); //true isEmpty({"key":"value"}); //false // jq版本 $.isEmptyObject({}); //true $.isEmptyObject({"key", "value"}); //false |
10. 获取对象的属性个数
1 2 3 4 5 6 7 8 9 10 |
Object.prototype.length = function() { var count = 0; for(var i in this){ count ++; } return count; }; // demo: var a = {a:1, b:2, c:3, d:4}; alert(a.length()); // 5 |