欢迎您来到懒之才-站长的分享平台!   学会偷懒,并懒出境界是提高工作效率最有效的方法!
首页 > 教程文档 > Js&Ajax > 读取和编写多重cookies

读取和编写多重cookies

2018-05-07 490 收藏 0 赞一个 0 真差劲 0 去评论

上一篇中我们学习了如何将大量的学习包含在一个cookie中.另一种方法是使用多重cookies.

保存多重cookies的方法很直观.每一个cookie都有一个名称.上一个例子中的cookie的名称是my_happy_cookie,我们可以这样:

var the_cookie ="my_happy_cookie=happiness_and_joy";
document.cookie =the_cookie;

要保存多重cookie,只需给每个cookie一个不同的名字.如果你要加入一个新的cookie,设置document.cookie 时并不会删除前面已经设置了的cookies,所以:

var the_cookie ="my_happy_cookie=happiness_and_joy";
document.cookie = the_cookie;
var another_cookie= "my_other_cookie=more_joy_more_happiness";
document.cookie = another_cookie;

现在你需要访问这两个cookies,有些复杂,所以你需要明了整个过程.假设你执行了上面的代码,现在想访问
my_happy_cookie.如果你查看document.cookie的内容,你会看到:

my_happy_cookie=happiness_and_joy;
my_other_cookie=more_joy_more_happiness;

这样很直观,但是如果你想访问某个特定的cookie则有些困难.

下面的代码可以帮助你找出某个特定的cookie:

function WM_readCookie(name)
{
//如果没有cookie则返回false或者取得值并返回该值
if(document.cookie == '')
    return false;
else
    return
unescape(WM_getCookieValue(name));
}
function WM_getCookieValue(name)
{
// Declare variables.
var firstChar,lastChar;
// Get the entire cookie string.
// (This may have other
name=value pairs in it.)
var theBigCookie = document.cookie;
// Grab
just this cookie from theBigCookie string.
// Find the start of
'name'.
firstChar = theBigCookie.indexOf(name);
// If you found it,
if(firstChar != -1)
{
// skip 'name' and '='.
firstChar +=
name.length + 1;
// Find the end of the value string (i.e. the next
';').
lastChar = theBigCookie.indexOf(';', firstChar);
if(lastChar == -1) lastChar = theBigCookie.length;
// Return the
value.
return theBigCookie.substring(firstChar, lastChar);
} else
{
// If there was no cookie, return false.
return false;
}
}

下面我们将学习一下cookies可以做的真正酷的内容。

返回主目录
暂无界面图片

一、推荐使用迅雷或快车等多线程下载软件下载本站资源。

二、未登录会员无法下载,登录后可获得更多便利功能,若未注册,请先注册。

三、如果服务器暂不能下载请稍后重试!总是不能下载,请点我报错 ,谢谢合作!

四、本站大部分资源是网上搜集或私下交流学习之用,任何涉及商业盈利目的均不得使用,否则产生的一切后果将由您自己承担!本站将不对任何资源负法律责任.如果您发现本站有部分资源侵害了您的权益,请速与我们联系,我们将尽快处理.

五、如有其他问题,请加网站设计交流群(点击这里查看交流群 )进行交流。

六、如需转载本站资源,请注明转载来自并附带链接

七、本站部分资源为加密压缩文件,统一解压密码为:www.aizhanzhe.com

大家评论