按名称查找cookie,然后使用javascript(无jquery)返回包含值的cookie
find a cookie by name then return the cookie including the value using javascript (no jquery)
是否有从document.cookie获取cookie的懒惰方法?
如果document.cookie是例如
abc_tracker=abc_visit_time%3D14352506807463%7 abc_campaign_id%3D0%7 abc_adgroup_id%3D10%7 abc_content_id%3B0%7 abc_mediate%3referration%7 abc_source%3Dblog.example.com%7 abc_activity%3none%7 abc_ad group%3none%7%abc_content%3none%7abc_term%3none%7 abc_match%3none+7abc_keyword%3Dbm9uZQ%3D%3D%7 abc_url%3DaHR0cDovL3d3dy5nb3 dheS5jb20vP19nYT0xLjM4NTgyNjkxLjEyMTg5NTk2Mi4xNDM1MjQ5OTU2%7Cabc_ref_url%3DaHR0cDovL2Jsb2cuZ293YXkuY29tL2dsb2JldHJvdHRpbmcvP19nYT0xLjM4NTgyNjkxLjEyMTg5NTk2Mi4xNDM1MjQ5OTU2;viewedOuibounceModal=true;LAstrongLIDEIN_VIEW=1435262685800;LAstrongLIDEIN_TYPE=聊天;c_m=vst_channel%3未定义%7 vst_keyword%3未定义%7%vst_referringDomain%3未定义%7vst_campaign%3未定义;__atuvc=11%7C25__atuvs=558d4a4029c67edc001;bloglanding=false;vst_tracker=id%3D143532282823%7Vid%3D1435256077463%7Medium%3Direct%7%entry_page_url%3Dhttp%253A%252F%252Fww.example.com%252F;__utma=148710120.217599539.14335256807.1435262252.14322883.3__utmb=148710120.18.1435322883__utmc=148710120__utmz=148710120.1435256807.1.1。utmcsr=blog.example.com|utmccn=(referral)|utmcmd=referral |utmct=/globetrotting/;__utmv=148710120.|1=vtime=1435256807463=1;abc_page_count=7;s_cc=真;s_fid=0B06C0537063F838-34C8F3D8F41ABCC8;s_sq=%5B%5B%5D%5D_ga=GA1.2.121895962.1435249956;__ar_v4=6UZ4UQZOCVE2RJGEKNO2AE%3A20150625%3A2%7LDQ27DO6EHARNHUHLKF3%3A20150225%3A41%7RYLYAX2L7NBGHDJQDTJYYL%3A20150725%3A41%7DYK2HT5L5FGJJFFVXXPM4K%3A20150025%3A38%7VIWWR64QNBEITBBPWIV5TWP%3A20150625%3A1
假设我想找到_ga饼干。阅读上面(底部附近)的cookie,整个事情就是_ga=GA1.2.121895962.1435249956
仅使用vanilla Javascript,最简单的检索方法是什么?
我陷入了一个试图使用火柴的兔子洞:
var bla = document.cookie.match(/_ga/) // returns the matched string.
bla.input // returns the whole thing.
基于输入"_ga",有什么简单的方法可以返回以下值:_ga=GA1.2.121895962.1435249956
首先,设置函数
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]
}
编辑通过REGEXP
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]
}
OR,仅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
尝试以下操作:
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);
在这里你可以找到更多的信息。
- 在jquery中为显示/隐藏设置cookie
- 使用jquery cookie的第三方cookie
- MVC C#从jQuery和JSON.stringify创建的cookie中读取值
- 如何使用Ajax/jQuery设置cookie
- 在jQuery AJAX请求后创建一个Cookie
- Jquery cookie - toggle box
- 如何使用 jquery.cookie 创建 if 语句
- 基于 jQuery Cookie 打开和关闭面板
- jQuery update cookie value
- 如何使用jquery读取cookie值
- jquery cookie,在清除浏览器历史记录后仍然设置
- 如何将jQuery克隆的文本输入保存到cookie或html5存储中
- 无法使用jquery设置cookie
- 为什么jQuery.cookie插件返回“[object object]”
- jQuery可以't使用jquery.cookie插件读取cookie
- jQuery cookie is not expring
- 存储数组到Cookie Jquery
- 如何加载一个DIV只有一次使用cookie jquery
- 时区作为cookie jquery PHP javascript
- 设置一个新的值给一个cookie创建一个重复的cookie - jquery cookie