欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

cookie操作的封装 常用判断方法的封装,cookie封装,'use strict'

来源: javaer 分享于  点击 48532 次 点评:258

cookie操作的封装 常用判断方法的封装,cookie封装,'use strict'


'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);        }    };//该片段来自于http://byrx.net
相关栏目:

用户点评