js设置cookie

function getCookie(name) {
    if (document.cookie.length>0) {
        var start = document.cookie.indexOf(name + '=');
        if (-1 != start) {
            start=start + name.length+1;
            var end = document.cookie.indexOf(';', start);
            if (-1 == end) end=document.cookie.length;
            return document.cookie.substring(start, end);
        }
    }
    return null;
}
function setCookie(key, value, path, second) {
    if (null == value) value = '';
    var p = '';
    if (path) p = '; path=' + path;
    var time = '';
    if (null != second) {
        var date = new Date();
        date.setTime(date.getTime() + second * 1000);
        time = '; expires=' + date.toGMTString();
    }
    document.cookie = key + '=' + value + p + time;
}
function deleteCookie(key) {
    document.cookie = key + '=; expires=' + new Date().toGMTString();
}

你可能感兴趣的:(js设置cookie)