未为寻宝脚本保存 Cookie

发布于 2024-12-08 13:08:54 字数 2072 浏览 0 评论 0原文

第一次海报,长期读者。

所以我需要创建一个快速而简单的寻宝游戏。这个想法是将一些值存储在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尸血腥色 2024-12-15 13:08:54

根据github中的README文件,需要定义如果您不希望 cookie 仅适用于创建它的页面,则 cookie 的有效路径:

定义cookie有效的路径。 默认的路径
cookie 是创建 cookie 的页面的路径(标准
浏览器行为)。
例如,如果您想让它可用
整个页面使用路径:'/'。如果你想删除cookie
对于特定路径,您需要在调用中包含该路径。

所以我认为它会是这样的:

$.cookie('treasure_hunt', treasure, { expires: 7, path: '/' });

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:

Define the path where cookie is valid. By default the path of the
cookie is the path of the page where the cookie was created (standard
browser behavior).
If you want to make it available for instance
across the entire page use path: '/'. If you want to delete a cookie
with a particular path, you need to include that path in the call.

So I think it would be something like this:

$.cookie('treasure_hunt', treasure, { expires: 7, path: '/' });

P.S. You're missing a semicolon at the end of your var treasure = new Array() declaration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文