javascript操作cookie
程序员文章站
2023-02-21 08:39:18
function SetCookie(name,value) //兩個參數,一個是cookie的名字,一個是值 { var Days = 30; //此 cookie 將被保存30天 var exp = new Date(); //new Date("December 31, 9998"); exp... ......
function setcookie(name,value) //兩個參數,一個是cookie的名字,一個是值
{
var days = 30; //此 cookie 將被保存30天
var exp = new date(); //new date("december 31, 9998");
exp.settime(exp.gettime() + days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.togmtstring();
}
function getcookie(name) //取cookies函數
{
var arr = document.cookie.match(new regexp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;
}
function delcookie(name)//刪除cookie
{
var exp = new date();
exp.settime(exp.gettime() - 1);
var cval=getcookie(name);
if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.togmtstring();
}
//示例
setcookie ("xiaoqi", "3")
alert(getcookie('xiaoqi'));