JS cookie 讀寫操做

/***
** 功能:  cookie操做對象
***/
var cookies = {
    /***
    ** 功能:  寫入cookie操做
    ** 參數:  name cookie名稱
    **         value cookie值 
    **         expires 過時時間
    **         path  路徑
    **         domain 域
    ***/
    set: function (name, value, expires, path, domain) {
        expires = new Date(new Date().getTime() + (((typeof expires == "undefined") ? 12 * 7200 : expires)) * 1000);
        var tempcookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "; path=/") +
        ((domain) ? "; domain=" + domain : "");
        (tempcookie.length < 4096) ? document.cookie = tempcookie : alert("The cookie is bigger than cookie lagrest");
    },

    /***
    ** 功能:  獲取cookie操做
    ** 參數:  name cookie名稱
    ***/
    get: function (name) {
        var xarr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
        if (xarr != null)
            return unescape(xarr[2]);
        return null;
    },

    /***
    ** 功能:  刪除cookie操做
    ** 參數:  name cookie名稱
    **         path  路徑
    **         domain 域
    ***/
    del: function (name, path, domain) {
        if (this.get(name))
            document.cookie = name + "=" +
            ((path) ? "; path=" + path : "; path=/") +
            ((domain) ? "; domain=" + domain : "") +
            ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
    },
    day: function (xd) {
        return xd * 24 * 3600;
    },
    hour: function (xh) {
        return xh * 3600;
    }
}
相關文章
相關標籤/搜索