通用js模块04:cookieUtils.js

/* ===========================================================
模块说明:对于cookie操作的一些补充功能
charset:gb2312
调用模块:
搜集整理:[email protected]
最后修订:2013-01-22
*/

function addCookie(name, value, expireDays, path, domain) {
    delCookie(name);
    expireDays = expireDays ? expireDays : 30;
    path = path ? path : "/";
    var date = new Date();
    date.setTime(date.getTime() + expireDays * 24 * 60 * 60 * 1000);
    var str = name + '=' + escape(value) + ';expires=' + date.toGMTString();
    if (path) str += ';path=' + path;
    if (domain) str += ';domain=' + domain;
    document.cookie = str;
}

function getCookie(name) {
    var arr = document.cookie.split(';');
    for (var i = 0; i < arr.length; i++) {
        var arr2 = arr[i].split('=');
        if (arr2[0] == name)
            return unescape(arr2[1]);
    }
    return null;
}

function delCookie(name) {
    var date = new Date();
    date.setTime(date.getTime() - 10000);
    document.cookie = name + "=; expires=" + date.toGMTString();
}

function getCookie2(name, defaultValue, expireDays, path, domain) {
    var ret = getCookie(name);
    if (!ret) {
        ret = defaultValue;
        addCookie(name, ret, expireDays, path, domain);
    }
    return ret;
}

你可能感兴趣的:(cookie)