如何通过特定键设置和获取cookie? [英] How to set and get cookie by perticular keys?
本文介绍了如何通过特定键设置和获取cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我通过'loggedinUser'设置cookie
if I set cookie by 'loggedinUser'
document.cookie="loggedinUser=dawood";
那么如何获得这个特定的'loggedInUser'cookie值?
then how to get this perticular 'loggedInUser' cookie value?
推荐答案
function getCookie(name)
{
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value != null) ? unescape(value[1]) : null;
}
更多信息 [ ^ ]
http://stackoverflow.com/questions/10730362/get-cookie-by-名称 [ ^ ] < br $>
-KR
More Info[^]
http://stackoverflow.com/questions/10730362/get-cookie-by-name[^]
-KR
var x = getCookie('name');
function getCookie(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;
}
alert(x);
这篇关于如何通过特定键设置和获取cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文