首页 > 代码库 > 设置和获取cookie

设置和获取cookie

function setCookie(name,value){

    document.cookie = name + "=" + encodeURI(value);

}

 

function getCookie(name){

    var str = document.cookie;

    if(!str || str.indexOf(name + "=") < 0){

       return null;

    }

    var cookies = str.split(";");

    for(var i=0; i < cookies.length; i++){

       var cookie = cookies[i];

       if(cookie.indexOf(name + "=") == 0){

           var value = http://www.mamicode.com/cookie.substring(name.length + 1);

           return decodeURI(value);

       }

    }

    return null;

}

设置和获取cookie