如何用js向cookie中保存数据、取数据?

JavaScript013

如何用js向cookie中保存数据、取数据?,第1张

用js向cookie中保存数据、获取数据的方法如下:\x0d\x0afunction GetCookieVal(offset)\x0d\x0a//获得Cookie解码后的值\x0d\x0a{\x0d\x0avar endstr = document.cookie.indexOf ("", offset)\x0d\x0aif (endstr == -1)\x0d\x0aendstr = document.cookie.length\x0d\x0areturn unescape(document.cookie.substring(offset, endstr))\x0d\x0a}\x0d\x0a\x0d\x0a//---------------------------\x0d\x0afunction SetCookie(name, value)\x0d\x0a//设定Cookie值\x0d\x0a{\x0d\x0avar expdate = new Date()\x0d\x0avar argv = SetCookie.arguments\x0d\x0avar argc = SetCookie.arguments.length\x0d\x0avar expires = (argc >2) ? argv[2] : null\x0d\x0avar path = (argc >3) ? argv[3] : null\x0d\x0avar domain = (argc >4) ? argv[4] : null\x0d\x0avar secure = (argc >5) ? argv[5] : false\x0d\x0aif(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ))\x0d\x0adocument.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("expires="+ expdate.toGMTString()))\x0d\x0a+((path == null) ? "" : ("path=" + path)) +((domain == null) ? "" : ("domain=" + domain))\x0d\x0a+((secure == true) ? "secure" : "")\x0d\x0a}\x0d\x0a\x0d\x0a//---------------------------------\x0d\x0afunction DelCookie(name)\x0d\x0a//删除Cookie\x0d\x0a{\x0d\x0avar exp = new Date()\x0d\x0aexp.setTime (exp.getTime() - 1)\x0d\x0avar cval = GetCookie (name)\x0d\x0adocument.cookie = name + "=" + cval + "expires="+ exp.toGMTString()\x0d\x0a}\x0d\x0a\x0d\x0a//------------------------------------\x0d\x0afunction GetCookie(name)\x0d\x0a//获得Cookie的原始值\x0d\x0a{\x0d\x0avar arg = name + "="\x0d\x0avar alen = arg.length\x0d\x0avar clen = document.cookie.length\x0d\x0avar i = 0\x0d\x0awhile (i 回答于 2022-12-11

这篇文章主要介绍了js文件Cookie存取值的使用,需要的朋友可以参考下

代码如下:

/*

Cookie工具

使用方法:

//存值

var

value

=

"7天"

tools.cookie("day",value,

{expires:7})

//将字符串:"7天"

"day"这个key保存到cookie中5天

//取值

var

v

=

tools.cookie("day")

//用

"day"

这个key从cookie取出值

*/

tools.cookie

=

function(name,

value,

options)

{

if

(typeof

value

!=

'undefined')

{

//

name

and

value

given,

set

cookie

options

=

options

||

{}

if

(value

===

null)

{

value

=

''

options.expires

=

-1

}

var

expires

=

''

if

(options.expires

&&

(typeof

options.expires

==

'number'

||

options.expires.toGMTString))

{

var

date

if

(typeof

options.expires

==

'number')

{

date

=

new

Date()

date.setTime(date.getTime()

+

(options.expires

*

24

*

60

*

60

*

1000))

}

else

{

date

=

options.expires

}

expires

=

'

expires='

+

date.toGMTString()

//

use

expires

//

attribute,

//

max-age

is

not

//

supported

by

IE

}

var

path

=

options.path

?

'

path='

+

options.path

:

''

var

domain

=

options.domain

?

'

domain='

+

options.domain

:

''

var

secure

=

options.secure

?

'

secure'

:

''

document.cookie

=

[

name,

'=',

encodeURIComponent(value),

expires,

path,

domain,

secure

].join('')

}

else

{

//

only

name

given,

get

cookie

var

cookieValue

=

null

if

(document.cookie

&&

document.cookie

!=

'')

{

var

cookies

=

document.cookie.split('')

for

(

var

i

=

0

i

<

cookies.length

i++)

{

var

cookie

=

jQuery.trim(cookies[i])

//

Does

this

cookie

string

begin

with

the

name

we

want?

if

(cookie.substring(0,

name.length

+

1)

==

(name

+

'='))

{

cookieValue

=

decodeURIComponent(cookie

.substring(name.length

+

1))

break

}

}

}

return

cookieValue

}

}