支持jQuery的cookie簡(jiǎn)單小插件

最近在做一個(gè)創(chuàng)業(yè)項(xiàng)目,用到了cookie,就簡(jiǎn)單用jquery封裝了一下,代碼很簡(jiǎn)單,看一眼就會(huì),不做過(guò)多解釋。下面是代碼

;(function ($) {
    $.extend({
        "setCookie": function (name, value, option) {
            var expires;
            if($.isPlainObject(name)){
                for(var key in name){
                    this.setCookie(k,name[k],value);
                }
            }else {
                var option= $.isPlainObject(option)?option:{time:option};
                var time=option.time;
                if($.type(time)==="string"&& time!=null){
                     expires=new Date(time);
                }else if($.type(time)==="number"){
                     expires=new Date(1000 * 60 * 60 * 24 *time+new Date());
                }
                if(expires!=null && "toGMTString" in expires){
                    expires=";expires="+expires.toGMTString()
                }
                document.cookie=name+"="+escape(value)+expires;
            }
        },
        "getCookie": function (name) {
            if(document.cookie.length>0){
                var endIndex;
                var startIndex=document.cookie.indexOf(name+"=");
                if(startIndex!=-1){
                    startIndex=startIndex+name.length+1;
                    endIndex=document.cookie.indexOf(";",startIndex);
                }
                if(endIndex==-1){
                    endIndex=document.cookie.length;
                }
                return unescape(document.cookie.substring(startIndex,endIndex));
            }
        },
        "removeCookie": function (name) {
            var names= $.type(name)==="array"?name:Array.prototype.slice.call(arguments);
            for(var i=0;i<names.length;i++){
                this.setCookie(names[i],"")
            }
        }
    })
})(jQuery);

分別是<code>setCookie、getCookie、removeCookie</code>,都用<code>$.extend</code>綁定到了jquery插件上,使用的時(shí)候直接調(diào)用<code>$.setCookie;$.getCookie;$.removeCookie</code>三個(gè)方法。

以后會(huì)繼續(xù)更新,添加新的方法。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容