gpt4 book ai didi

javascript - 如何在javascript中从cookie中获取值

转载 作者:行者123 更新时间:2023-11-28 07:46:16 24 4
gpt4 key购买 nike

我需要什么:

  • 我需要使用 cookie 中的 user 和 evnt_id。

    document.cookie

    字符串:

    " country=India; countryCode=IN; __gads=ID=369fde524de77341:T=1417077062:S=ALNI_MakhfG3fYeRIwt9Uw4xSUE8m-2hHw; fbm_172404889604820=base_domain=.10times.com; PHPSESSID=4e467l46efdmvc9vktknod85b2; evnt_id=9; industry=74; evnt_name=India International Trade Fair; evnt_url=iitf; km_ai=jE0JIU3hamljHh0DrWRC%2FR50QNI%3D; km_lv=x; __zlcmid=SAeGECzmK8Nrve; user_flag=2; user=13530767; _ga=GA1.2.1767513107.1417003918; linkedin_oauth_ro57ogahnixy_crc=null; km_uq=; kvcd=1418207265523; _ga=GA1.3.342868374.1416999745; id=13530767; email=afeef1915%40yahoo.com; name=Mohd+Afeef; image_flag=http%3A%2F%2Fgraph.facebook.com%2F100001729894039%2Fpicture".
  • 我需要获取

    • 用户。
    • 来自 cookie 的 event_id。

我尝试过的代码:

function setCookie(key, value) {  
var expires = new Date();
expires.setTime(expires.getTime() + 31536000000); //1 year
document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();

}

function getCookie(key) {
var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');

return keyValue ? keyValue[2] : null;
}

setCookie('evnt_id','evnt_id');
alert(getCookie('evnt_id'));
  • 我面临的问题是无法从 cookie 获取用户和 event_id。
  • 我使用的是 symphony 框架,需要在 twig 文件中实现。

最佳答案

这个问题之前已经解决了。不要重新发明轮子:)

我建议您阅读有关 cookie 的内容:http://www.quirksmode.org/js/cookies.html

下面的代码来自该文章。

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;
}

console.log(readCookie('evnt_id')); //9
console.log(readCookie('user')); //13530767

关于javascript - 如何在javascript中从cookie中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27399977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com