这似乎是一个相当简单的问题......我试图用cookie a
中的数据覆盖cookie b
中的数据但不更改任何cookie的名称。 Cookie b
包含特定用户的默认值,而Cookie a
存储用户当前选择。
我已经在网上看了一眼并且在黑暗中拍摄了几张照片,但我没有线索!我尝试过的所有内容都会覆盖整个Cookie,我只希望更改Cookie a
中的数据,而不更改Cookie的名称或任何其他Cookie的数据
这可能吗?如果是这样,我该如何完成这项任务?
提前谢谢!
答案 0 :(得分:0)
这是代码。它的评论足以回答这个问题:
//first, get the "b" cookie's value
var cooks=document.cookie;
//just the "b" part of the cookie string
var bStartIndex=cooks.indexOf('b=')+2; //get the start of the b part of the string
var bEndIndex=cooks.indexOf(';',bStartIndex); //and the end of it
//and then extract the actual variable value from it
var b=cooks.slice(bStartIndex,bEndIndex!==-1?bEndIndex:null); //in case b was the last var, there would be no ; after it, so bEndIndex would be -1
//now, we set a to that
document.cookie='a='+b; //I don't THINK this will overwrite other cookies
我没有测试过,但它应该可以使用。告诉我,如果没有,我会尝试解决它
如果您计划在生产中使用此库,那么可能存在可以使这更容易的库