js使用cookie数组传值

JavaScript019

js使用cookie数组传值,第1张

下面是代码示例//赋值给cookiefunction

setcookies(name,value)

{

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)

+"expire*="+

**p.togmtstring()

}

//读取cookiefunction

getcookies(name)

{

var

arr

=

document.cookie.match(new

regexp("(^|

)"+name+"=([^]*)(|$)"))

if(arr

!=

null)

return

unescape(arr[2])

return

null

}

//删除cookiefunction

delcookies(name)

{

var

exp

=

new

date()

exp.settime(exp.gettime()

-

1)

var

cval=getcookie(name)

if(cval!=null)

document.cookie=name

+"="+cval+"expire*="+**p.togmtstring()

}

JS设置cookie:

假设在A页面中要保存变量username的值("jack")到cookie中,key值为name,则相应的JS代码为:

document.cookie="name="+username

JS读取cookie:

假设cookie中存储的内容为:name=jackpassword=123

则在B页面中获取变量username的值的JS代码如下:

var username=document.cookie.split("")[0].split("=")[1]