js读取写入cookie

//写入cookie
function setCookie(name,value)
{
    var Days = 30; //此 cookie 将被保存 30 天
    var exp = new Date();
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}

//读取cookie
function getCookie(name)
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr)
        return unescape(arr[2]);
    return null;
}


//读取cookie decode
function getCookie2(name)
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr)
        return decodeURIComponent(arr[2]);
    return null;
}

案例测试

js读取写入cookie_第1张图片
Paste_Image.png

你可能感兴趣的:(js读取写入cookie)