首页 > 代码库 > js创建和获取cookie

js创建和获取cookie

创建cookie

document.cookie=like=1;   //创建 cookie键名和值var str = document.cookie;

 

获取cookie

读取cookie
function getCookie(c_name){
if (document.cookie.length>0){ c_start=document.cookie.indexOf(c_name + "=")if (c_start!=-1){ c_start=c_start + c_name.length+1 c_end=document.cookie.indexOf(";",c_start)if (c_end==-1) c_end=document.cookie.lengthreturn unescape(document.cookie.substring(c_start,c_end))} }return ""}

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

 

创建cookie
function setcookie(){ document.cookie
=like=ok; }alert(getcookie(like))