java cookie操作的封装 常用判断方法的封装
2018-07-20 来源:open-open
'use strict';//启用严格模式
var oap={},obj_type={},core_toString=obj_type.toString,hasOwn = obj_type.hasOwnProperty;;
var type_arr = "Boolean Number String Function Array Date RegExp Object Error".split(" ");
for(var i in type_arr){
obj_type[ "[object " + type_arr[i] + "]" ] = type_arr[i].toLowerCase();
};
//常用工具 判断方法 对象
oap={
type:function(obj){
if ( obj == null ) {
return String( obj );
}
return (typeof obj === "object" || typeof obj === "function")?(obj_type[ core_toString.call(obj) ] || "object") :typeof obj;
},
isStr:function(obj){
return oap.type(obj)==="string";
},
isFn:function(obj){
return oap.type(obj)==="function";
},
isObj:function(obj){
return oap.type(obj) === "object";
},
isDate:function(obj){
return oap.type(obj) === "date";
},
isEmptyObj:function(obj){
var name;
for ( name in obj ) {
return false;
}
return true;
},
isNum:function(obj){
return !isNaN( parseFloat(obj) ) && isFinite( obj );
},
isArr:function(obj){
return oap.type(obj) === "array";
},
trim:function(obj){
return obj.replace(/^\\s+|\\s+$/g, "");
},
now:function(){
return new Date().getTime();
},
log:function(str){
if(console && console.log){
console.log(str);
}
}
};
var _cookie = {
set:function(name, value){
var expires = (arguments.length > 2) ? arguments[2] : null,strExpires = "";
if(expires && oap.isDate(expires)){
strExpires = "; expires=" + expires.toGMTString();
}else if(expires && oap.isObj(expires)){
var nD = oap.now(),
nObj = {
day:0,hours:0,minutes:0,seconds:0
},
dArr={
secondes:1000,
minutes:1000*60,
hours:1000*60*60,
day:1000*60*60*24
},
_val;
nObj = oap.extend(nObj,expires);
for(var n in expires){
_val = nObj[n];
if(oap.isNum(_val) && _val>0){
nD = nD + _val * dArr[n];
}
}
if(oap.isNum(nD) && nD>0){
nD = new Date(nD);
strExpires = "; expires=" + nD.toGMTString();
}
}else{
strExpires = "";
}
document.cookie = name + "=" + encodeURIComponent(value) + strExpires + ";path=/" ;
},
get:function(name){
var value = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
if (value != null) {
return decodeURIComponent(value[2]);
} else {
return null;
}
},
remove:function(name){
var expires = new Date();
expires.setTime(expires.getTime() - 1000 * 60);
this.set(name, "", expires);
}
};
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:php简体繁体转换
最新资讯
热门推荐