javascript - 未为寻宝脚本保存 Cookie

标签 javascript jquery cookies

第一次发帖,长期读者。

所以我需要创建一个快速而简单的寻宝游戏。这个想法是将一些值存储在 cookie 中,当用户单击具有匹配 id 的元素时,更新以下值。它还没有在那里,但是当这个人找到所有 8 个(在本例中)时,它会触发一些东西。

现在,我已经让它大部分工作了,但我遇到了一个大问题。 Cookie 不会逐页存储。如果我正在处理单个测试页,那就没问题了 - 创建 cookie,从其中提取值并将其放入检查数组中,并且计数器正确递增。另外,当 cookie 更新时,新值会被很好地存储。

但是,当我转到另一个页面并检查 cookie 时 - 它已经消失了。现在我检查了一下 cookies 是否真的被扔进去了,是的,它就在那里。但是有多个 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 });
    }

}

});

最佳答案

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

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.

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

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

附注您的 var Treasure = new Array() 声明末尾缺少一个分号。

关于javascript - 未为寻宝脚本保存 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7651822/

相关文章:

javascript - 为什么 jquery 日期选择器插件不起作用

javascript - 如何使用 hammer.js jQuery 插件设置不同的速度来识别滑动?

javascript - 单击链接 -> 设置 cookie -> 加载附加样式表

javascript - 点击时设置 Cookie

javascript - 当使用 JavaScript 将光标设置为节点的开头时,Google Chrome 将光标移动到前一个节点的末尾

javascript - 需要 : HTML 5/Javascript UI Component to do "Matching"/"Data Mapping" UI

javascript - 更改突出显示颜色

javascript - jQuery:如何获取另一个元素内某个元素的类?

javascript - Cookie 值中的 native JavaScript 日期格式?

javascript - 在 document.write 中运行函数,其中 document.write 在 window.open 中