未为寻宝脚本保存 Cookie
第一次海报,长期读者。
所以我需要创建一个快速而简单的寻宝游戏。这个想法是将一些值存储在 cookie 中,当用户单击具有匹配 id 的元素时,更新以下值。它还没有在那里,但是当这个人找到所有 8 个(在本例中)时,它会触发一些东西。
现在,我已经让它大部分工作了,但我遇到了一个大问题。 Cookie 不会逐页存储。如果我正在处理单个测试页,那就没问题了 - 创建 cookie,从其中提取值并将其放入检查数组中,并且计数器正确递增。此外,当 cookie 更新时,新值也可以很好地存储。
然而,当我转到另一个页面并检查 cookie 时,它已经消失了。现在我检查了一下饼干是否真的被扔进去了,是的,它就在那里。但是有多个 cookie 都具有相同的名称(它们正在更新值 - 但同样,它只是页面到页面,因此没有累积效应)。
所以,我想我不明白为什么当我调用 cookie 并重置它时,它不会影响单个 cookie,而是创建一个新的 cookie?
哦,我正在使用 jQuery 1.4.2 和 jQuery cookie 插件。
// if not present create default array and store in cookie cookie
if($.cookie('treasure_hunt') == null) {
var treasure = new Array(
"arteContactEn",0,
"arteCorpEn",0,
"arteServicesEn",0,
"arteRoomsEn",0,
"arteDiningEn",0,
"artePackEn",0,
"arteHotelEn",0,
"arteHomeEn", 0
)
$.cookie('treasure_hunt', treasure, { expires: 7 });
}
// capture cookie info
var treasureFound = $.cookie('treasure_hunt');
// create check array and parse values into it
var treasureCheck = new Array();
var treasureCheck = treasureFound.split(',');
// function checks array and tallies total found items
function checkFound() {
var foundItems = 0;
for(var i=0; i<treasureCheck.length; i++) {
var value = treasureCheck[i];
if(value == 1) {
++foundItems;
}
}
// writes found items to span on page
$('.thProgress').text(foundItems);
}
// check number artefacts and write to page
var treasureTotal = treasureCheck.length / 2;
$('.thTotal').text(treasureTotal);
// on click checks element id against array and, if found, marks the following value as 1
$('.artefact-right, .artefact-left').click(function(){
var artefactID = $(this).attr('id');
for(var e=0; e<treasureCheck.length; e++) {
var matchID = treasureCheck[e];
var k = e + 1;
if(matchID == artefactID) {
treasureCheck[k] = 1;
// checks for total found items
checkFound();
// updates cookie with new array
$.cookie('treasure_hunt', treasureCheck, { expires: 7 });
}
}
});
first time poster, long time reader.
So I needed to create a quick and simple treasure hunt. The ideas was to store some values in a cookie and, when the user click on an element with a matching id, update the following value. It's not in there yet but when the person has found all 8 (in this case) it will trigger something.
Now, I've gotten it to mostly work but I'm running in to a big problem. The cookie is not being stored from page to page. If I'm working off a single test page it's just fine - the cookie is created, values are pulled from it and put in to a check array, and the counter increments correctly. Also, when the cookie is updated the new values are being stored just fine.
However, when I go to another page and check for the cookie - it's gone. Now I checked to see if the cookie was actually getting dropped in and yes, it's there. But there are multiple cookies all with the same name (they are updating values - but again it's only page to page so no cumulative effect).
So, I guess I don't understand why when I call the cookie and reset it, it's not affecting a single cookie but creating a new one?
Oh, I'm using jQuery 1.4.2 and the jQuery cookie plugin.
// if not present create default array and store in cookie cookie
if($.cookie('treasure_hunt') == null) {
var treasure = new Array(
"arteContactEn",0,
"arteCorpEn",0,
"arteServicesEn",0,
"arteRoomsEn",0,
"arteDiningEn",0,
"artePackEn",0,
"arteHotelEn",0,
"arteHomeEn", 0
)
$.cookie('treasure_hunt', treasure, { expires: 7 });
}
// capture cookie info
var treasureFound = $.cookie('treasure_hunt');
// create check array and parse values into it
var treasureCheck = new Array();
var treasureCheck = treasureFound.split(',');
// function checks array and tallies total found items
function checkFound() {
var foundItems = 0;
for(var i=0; i<treasureCheck.length; i++) {
var value = treasureCheck[i];
if(value == 1) {
++foundItems;
}
}
// writes found items to span on page
$('.thProgress').text(foundItems);
}
// check number artefacts and write to page
var treasureTotal = treasureCheck.length / 2;
$('.thTotal').text(treasureTotal);
// on click checks element id against array and, if found, marks the following value as 1
$('.artefact-right, .artefact-left').click(function(){
var artefactID = $(this).attr('id');
for(var e=0; e<treasureCheck.length; e++) {
var matchID = treasureCheck[e];
var k = e + 1;
if(matchID == artefactID) {
treasureCheck[k] = 1;
// checks for total found items
checkFound();
// updates cookie with new array
$.cookie('treasure_hunt', treasureCheck, { expires: 7 });
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据github中的README文件,需要定义如果您不希望 cookie 仅适用于创建它的页面,则 cookie 的有效路径:
所以我认为它会是这样的:
PS 你在
var Treasure = new Array()
声明的末尾缺少一个分号。According to the README file in github, you need to define the path the cookie is valid for if you don't want it to be only for the page it was created on:
So I think it would be something like this:
P.S. You're missing a semicolon at the end of your
var treasure = new Array()
declaration.