js cookie 存的对象,取出来怎么用

js cookie 存的对象,取出来怎么用,第1张

这篇文章主要介绍了js文件Cookie存取值的使用,需要的朋友可以参考下
代码如下:
/
Cookie工具
使用方法:
//存值
var
value
=
"7天";
toolscookie("day",value,
{expires:7});
//将字符串:"7天"

"day"这个key保存到cookie中5天
//取值
var
v
=
toolscookie("day");
//用
"day"
这个key从cookie取出值
/
toolscookie
=
function(name,
value,
options)
{
if
(typeof
value
!=
'undefined')
{
//
name
and
value
given,
set
cookie
options
=
options
||
{};
if
(value
===
null)
{
value
=
'';
optionsexpires
=
-1;
}
var
expires
=
'';
if
(optionsexpires
&&
(typeof
optionsexpires
==
'number'
||
optionsexpirestoGMTString))
{
var
date;
if
(typeof
optionsexpires
==
'number')
{
date
=
new
Date();
datesetTime(dategetTime()
+
(optionsexpires

24

60

60

1000));
}
else
{
date
=
optionsexpires;
}
expires
=
';
expires='
+
datetoGMTString();
//
use
expires
//
attribute,
//
max-age
is
not
//
supported
by
IE
}
var
path
=
optionspath

';
path='
+
optionspath
:
'';
var
domain
=
optionsdomain

';
domain='
+
optionsdomain
:
'';
var
secure
=
optionssecure

';
secure'
:
'';
documentcookie
=
[
name,
'=',
encodeURIComponent(value),
expires,
path,
domain,
secure
]join('');
}
else
{
//
only
name
given,
get
cookie
var
cookieValue
=
null;
if
(documentcookie
&&
documentcookie
!=
'')
{
var
cookies
=
documentcookiesplit(';');
for
(
var
i
=
0;
i
<
cookieslength;
i++)
{
var
cookie
=
jQuerytrim(cookies[i]);
//
Does
this
cookie
string
begin
with
the
name
we
want
if
(cookiesubstring(0,
namelength
+
1)
==
(name
+
'='))
{
cookieValue
=
decodeURIComponent(cookie
substring(namelength
+
1));
break;
}
}
}
return
cookieValue;
}
};


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13037345.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-29
下一篇 2023-05-29

发表评论

登录后才能评论

评论列表(0条)

保存