按名称查找cookie,然后使用javascript(无jquery)返回包含值的cookie [重复]
Posted
技术标签:
【中文标题】按名称查找cookie,然后使用javascript(无jquery)返回包含值的cookie [重复]【英文标题】:find a cookie by name then return the cookie including the value using javascript (no jquery) [duplicate] 【发布时间】:2015-09-13 11:15:30 【问题描述】:有没有偷懒的方法从 document.cookie 中获取 cookie?
如果 document.cookie 是例如
abc_tracker = abc_visit_time%3D1435256807463%7Cabc_campaign_id%3D0%7Cabc_adgroup_id%3D0%7Cabc_content_id%3D0%7Cabc_medium%3Dreferral%7Cabc_source%3Dblog.example.com%7Cabc_campaign%3Dnone%7Cabc_adgroup%3Dnone%7Cabc_content%3Dnone%7Cabc_term%3Dnone%7Cabc_match %3Dnone%7Cabc_keyword%3Dnone%7Cabc_ref_keyword%3Dbm9uZQ%3D%3D%7Cabc_url%3DaHR0cDovL3d3dy5nb3dheS5jb20vP19nYT0xLjM4NTgyNjkxLjEyMTg5NTk2Mi4xNDM1MjQ5OTU2%7Cabc_ref_url%3DaHR0cDovL2Jsb2cuZ293YXkuY29tL2dsb2JldHJvdHRpbmcvP19nYT0xLjM4NTgyNjkxLjEyMTg5NTk2Mi4xNDM1MjQ5OTU2; 查看OuibounceModal=true; LAST_SLIDEIN_VIEW=1435262685800; LAST_SLIDEIN_TYPE=聊天; c_m=vst_channel%3Dundefined%7Cvst_keyword%3Dundefined%7Cvst_referringDomain%3Dundefined%7Cvst_campaign%3Dundefined; __atuvc=11%7C25; __atuvs=558d4a4029c67edc001;博客登陆=假; vst_tracker=id%3D1435322882823%7Cvid%3D1435256807463%7Cmedium%3Ddirect%7Centry_page_url%3Dhttp%253A%252F%252Fwww.example.com%252F; __utma=148710120.217599539.1435256807.1435262252.1435322883.3; __utmb=148710120.18.10.1435322883; __utmc=148710120; __utmz=148710120.1435256807.1.1.utmcsr=blog.example.com|utmccn=(referral)|utmcmd=referral|utmcct=/globetrotting/; __utmv=148710120.|1=vtime=1435256807463=1; abc_page_count=7; s_cc=真; s_fid=0B06C0537063F838-34C8F3D8F41ABCC8; s_sq=%5B%5BB%5D%5D; _ga=GA1.2.121895962.1435249956; __ar_v4 = 6UZ4UQZOCVE2RJGEKNO2AE%3A20150625%3A2%7CLDQ27DO6EFHARNHUHZLKF3%3A20150625%3A41%7CRYLYAX2L7NBGHDJQDTJYYL%3A20150625%3A41%7CDYK2HT5L5FGJNJFVXXPM4K%3A20150625%3A38%7CVIWWR64QNBEITBPWIV5TWP%3A20150625%3A1 P>
假设我想找到 _ga cookie。阅读上面的 cookie(靠近底部)就是_ga=GA1.2.121895962.1435249956
仅使用 vanilla javascript,最简单的检索方法是什么?
我掉进了一个尝试使用 match 的兔子洞:
var bla = document.cookie.match(/_ga/) // returns the matched string.
bla.input // returns the whole thing.
根据输入“_ga”,有什么简单的方法可以返回以下值:_ga=GA1.2.121895962.1435249956
【问题讨论】:
【参考方案1】:首先,你设置函数
function createCookie(name,value,days)
if (days)
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
function readCookie(name)
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++)
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
return null;
function eraseCookie(name)
createCookie(name,"",-1);
那么你只需要读取并设置它。
//Setting
createCookie('_ga','9saua9s8d89w',7);
//Reading
var x = readCookie('_ga')
if (x)
//[do something with x]
编辑 通过正则表达式
function readCookie(cookieName)
var re = new RegExp('[; ]'+cookieName+'=([^\\s;]*)');
var sMatch = (' '+document.cookie).match(re);
if (cookieName && sMatch) return unescape(sMatch[1]);
return '';
然后
var x = readCookie('_ga');
if (x)
//[do something with x]
或者,只是 REGEXP 代码
var cookie = "_ga"
var re = new RegExp('[; ]'+cookie+'=([^\\s;]*)');
var cookieVal = unescape((' '+document.cookie).match(re)[1]);
console.log(cookieVal); //GA1.2.116321536.1432242890
【讨论】:
感谢您的回答。我知道这会奏效。我应该更明确一点:我希望在 match() 中使用正则表达式的更懒惰(更少行)的解决方案 看我的答案编辑(:现在你得到了你想要的。 这太好了,谢谢!【参考方案2】:尝试以下方法:
var _ga = document.cookie.split(';').map(function(x)
return x.trim().split(/(=)/);
).reduce(function(a, b)
a[b[0]] = a[b[0]] ? a[b[0]] + ', ' + b.slice(2).join('') :
b.slice(2).join('');
return a;
, )["_ga"]; // <--- cookie you are looking for.
console.log(_ga);
Here你可以找到更多信息。
【讨论】:
以上是关于按名称查找cookie,然后使用javascript(无jquery)返回包含值的cookie [重复]的主要内容,如果未能解决你的问题,请参考以下文章