js 操作 cookie

來源:互聯網
上載者:User

js 操作 cookie

$(function(){    /**     * version 1.0     * 功能:此檔案旨在通過js操作cookie,包括cookie的讀和寫     * 作者:郭軍周     * Email:853547029@qq.com     * 使用舉例:     *      window.__cookie.setCookie(string key,string value,int time);     *      window.__cookie.getCookie(string key);     * 幾點說明:     *      1:cookie 以字串的形式儲存在 document.cookie 中     *      2:window 對象持有 document 對象的唯讀引用。這決定了,我們可以通過 window.document.cookie 的     *          方式,擷取這個 cookie 字串,但這種方式不能夠實現對cookie的賦值。即:     *          window.document.cookie = 'aaa'; 並不能改變cookie     *          此時,我們應該通過 document.cookie = 'aaa'; 的方式進行對cookie的修改     *      3:對於 cookie 到期時間的幾點說明:     *          3.1:這個時間,以“毫秒”為單位,1000ms = 1s;     *          3.2:只有當 cookie 中的該“鍵值對”還未到期時,document.cookie 中才有該鍵值對的字串形式。     *              簡言之,在擷取 cookie 時,並不需要手動對到期時間進行驗證,因為,如果到期,將得不到此值。     *          3.3:至於,該鍵值對,是否到期,以及到期的處理,是由瀏覽器本身來實現的。     *      4:對於設定 cookie 時 key,value,expire 的幾點說明:     *          4.1:推薦 window.__cookie.setCookie(string,string,int)。下面的調用方式,可能造成各種莫名的問題,不建議。     *              window.__cookie.setCookie(1,2,30000);     *              window.__cookie.setCookie('',2,30000);     *              window.__cookie.setCookie(2,'',30000);     *              window.__cookie.setCookie(3,'',30000);     *              window.__cookie.setCookie('_','',30000);     *              window.__cookie.setCookie(-1,'',30000);     *              window.__cookie.setCookie(';','',30000);     *          4.2:setCookie 和 getCookie 的參數說明:     *              並未對其參數做過多驗證,比如:key 應該滿足變數的命名規範。因為,php確實能夠處理關聯陣列。     **/    window.__cookie = {        'cookieEnabled' : window.navigator.cookieEnabled,        'setCookie' : function(key,value,expire){            if(!this.cookieEnabled){                alert('瀏覽器未開啟或者cookie功能!');                return false;            }            var exdate = new Date();            exdate.setTime(exdate.getTime()+expire);            var c_nvalue=escape(value) + ((expire==null) ? "" : "; expires="+exdate.toUTCString());            if(this.getCookie(key) == null){                document.cookie = key + "=" + c_nvalue + document.cookie;            }else{                var c_value = window.document.cookie;                var c_start = c_value.indexOf(" " + key + "=");                if(c_start == -1){                    c_start = c_value.indexOf(key + "=");                }                c_start = c_value.indexOf("=", c_start) + 1;                var c_end = c_value.indexOf(";", c_start);                if(c_end == -1){                    c_end = c_value.length;                }                document.cookie = c_value.substring(0,c_start) + c_nvalue + c_value.substring(c_end,c_value.length);            }        },        'getCookie' : function(c_name){            if(!this.cookieEnabled){                alert('瀏覽器未開啟或者cookie功能!');                return false;            }            var c_value = window.document.cookie;            var c_start = c_value.indexOf(" " + c_name + "=");            if(c_start == -1){                c_start = c_value.indexOf(c_name + "=");            }            if(c_start == -1){                c_value = null;            }else{                c_start = c_value.indexOf("=", c_start) + 1;                var c_end = c_value.indexOf(";", c_start);                if(c_end == -1){                    c_end = c_value.length;                }                c_value = unescape(c_value.substring(c_start,c_end));            }            return c_value;        }    }});

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.