js 操作 cookie
//设置cookie
function setCookie(cookieName,coojieValue,cookieHour)
{
if(cookieName==null)
{
alert("请填写cookie名字");
return false;
}
else if(coojieValue==null)
{
alert("请填写cookie值");
return false;
}
else if(cookieHour!=null)
{
var str=cookieName+"="+escape(coojieValue);
var date=new Date();
date.setTime(date.getTime()+3600*1000*cookieHour);
str+=";expires="+date.toGMTString();
document.cookie=str;
alert("添加cookie成功");
return ;
}
else
{
var date=new Date();
date.setTime(date.getTime()+3600*1000);
document.cookie=cookieName+"="+coojieValue+";expires="+date.toGMTString();
alert("添加cookie成功");
return ;
}
}
//获取cookie
function getCookie(cookieName)
{
var cookieObj=document.cookie.split('; ');
for(var i=0; i<cookieObj.length; i++)
{
var cookie=cookieObj[i];
var cookieList=cookie.split('=');
var name=cookieList[0];
var value=cookieList[1];
if(name==cookieName)
{
alert(name);
}
}
}
//只有一个cookie时候清除cookie
function delCookie(cookieName)
{
var cookieObj=document.cookie.split('; ');
var date=new Date();
date.setTime(date.getTime()-1000)
document.cookie=cookieName+"=;expires="+date.toGMTString();
}
//获取所有cookie
function getAllCookie()
{
var cookies=document.cookie;
if(cookies=="")
{
alert("没有cookie");
}
else
{
alert(document.cookie);
}
}
//多个cookie获取特定的cookie值
function getTheOne(cookieName)
{
var cookies=document.cookie.split('; ');
for(var i=0; i<cookies.length; i++)
{
var name=cookies[i].split('=')[0];
var value=cookies[i].split('=')[1];
if(name==cookieName)
{
alert(value);
}
}
}
//清除所有cookie
function clearAllCookie()
{
var cookies=document.cookie.split('; ');
try
{
for(var i=0;i<cookies.length;i++)
{
var cookie=cookies[i];
var cookieName=cookie.split('=');
var date=new Date();
date.setTime(date.getTime()-10000);
document.cookie=cookieName[0]+"="+";expires="+date.toGMTString();
}
alert("清除cookie成功");
}
catch(e)
{
alert("清除cookie失败");
}
}