按名称读取JavaScript Cookie [英] Read a javascript cookie by name
问题描述
我已使用
设置了Cookie。 document.cookie =
'MYBIGCOOKIE ='+ value +
'; expires ='+ now.toGMTString()+
'; path = /';
现在这个网站上设置了5到10个Cookie,是否有办法检查值
if(document.cookie.MYBIGCOOKIE =='1'){
alert it is 1')
}
,Javascript的cookie语法是没有那么好的那样。
当你尝试阅读 document.cookie
,你会得到一个包含所有cookie的字符串。您必须解析字符串,用分号;
字符分隔。而不是自己写,网上有很多版本可用。我最喜欢的是 quirksmode.org上的。这会给你 createCookie
, readCookie
和 deleteCookie
/ p>
I have set a cookie using
document.cookie =
'MYBIGCOOKIE=' + value +
'; expires=' + now.toGMTString() +
'; path=/';
Now there are between 5 and 10 cookies set on this site, is there a way to check the value ofthis cookie by name.
if (document.cookie.MYBIGCOOKIE == '1') {
alert('it is 1')
}
Unfortunately, Javascript's cookie syntax is nowhere near as nice as that. In fact, in my opinion, it's one of the worst designed parts.
When you try to read document.cookie
, you get a string containing all the cookies set. You have to parse the string, separating by the semicolon ;
character. Rather than writing this yourself, there are plenty of versions available on the web. My favourite is the one at quirksmode.org. This gives you createCookie
, readCookie
and deleteCookie
functions.
这篇关于按名称读取JavaScript Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!