简体   繁体   English

无法在Chrome中设置document.cookie

[英]document.cookie can't be set in Chrome

I want to save a cookie using simple javascript. 我想使用简单的JavaScript保存Cookie。 So I went to w3 and they have a ready made function to save a cookie. 所以我去了w3 ,他们有一个现成的函数来保存cookie。 When I tried this code in firefox it worked exactly as expected, but in Chrome setting the cookie had no effect. 当我在firefox中尝试此代码时,它的工作与预期的完全一样,但是在Chrome中,cookie无效。 I saw other questions on this site where the cookie was deleted because of a lack of expiredate, but I both set a date for a few days later and document.cookie never gets set. 我在这个网站上看到了其他问题,由于缺少到期日期而删除了cookie,但是我都将日期设置为几天,而document.cookie从未设置。 I walked through the code line by line in the debugger, but the value of document.cookie stayed an empty string even right after the line : 我在调试器中逐行浏览了代码,但是document.cookie的值即使在该行之后也保持为空字符串:

document.cookie = cname + "=" + cvalue + ", " + expires + ", path=/ ;";

I've tried this with and without a path and or expiration date, but nothing seems to have an effect. 我已经尝试过在没有路径和/或到期日期的情况下进行此操作,但是似乎没有任何效果。 What should I do? 我该怎么办?

Some extra information about my files as requested by @AndrewL64: I made a 1 page html game. @ AndrewL64请求有关我的文件的一些额外信息:我做了一个1页的html游戏。 I have a index.html file, a mainstyle.css file and a main.js file for the script. 我有该脚本的index.html文件,mainstyle.css文件和main.js文件。 In my script I use JQuery to manipulate the DOM elements. 在我的脚本中,我使用JQuery来操作DOM元素。 I put the code in the on page load event like this: 我将代码放在页面加载事件中,如下所示:

//==================On Page Load ===================================
$(document).ready(function () {

    $("#gameContainer").hide();
    $("#mainContainer").hide();
    //$("#startContainer").hide();
    $("#skillsContainer").hide();


    prepareGame();
    //startGame();

    /*var cookieName = "sp_str_cookie_save1";
    var saveString = "some value";
    setCookie(cookieName, saveString, 10);
    var cookieResult = getCookie(cookieName);*/
    const cname = "someCookie";
    const expires = "Thu, 2 Aug 2020 20:47:11 UTC";
    const cvalue = "someValue";

    document.cookie = cname + "= " + cvalue + "; " + "expires=" + expires + "; " + "path=/";

});

I found the answer in another Stackoverflow question here . 我在这里的另一个Stackoverflow问题中找到了答案。 In short, some browsers don't set cookies when opening a html file locally. 简而言之,某些浏览器在本地打开html文件时不会设置cookie。 For example Chrome doesn't, but Firefox does. 例如,Chrome没有,但Firefox有。 So test cookies in Firefox if you are working offline. 因此,如果您离线工作,请在Firefox中测试Cookie。

The parameters should be separated with a semicolon and a white-space , not a comma . 参数应该用分号空格 隔开 ,而不是逗号


Change your current code from this: 从此更改当前代码:

document.cookie = cname + "=" + cvalue + ", " + expires + ", path=/ ;";

To this: 对此:

document.cookie = cname + "= " + cvalue + "; " + expires + "; " + "path=/";

Check and run this JSFiddle which has the following Code Snippet and then check your browser cookies to see the new cookie added: 检查并运行具有以下代码片段的 JSFiddle ,然后检查您的浏览器cookie以查看添加的新cookie:

 const cname = "someCookie"; const expires = "Thu, 2 Aug 2020 20:47:11 UTC"; const cvalue = "someValue"; document.cookie = cname + "= " + cvalue + "; " + "expires=" + expires + "; " + "path=/"; 


NB The above Code Snippet won't set the cookie since the snippet environment is just a sandbox and lacks the 'allow-same-origin' flag. 注意 :上面的代码段不会设置cookie,因为该代码段环境只是一个沙箱,并且缺少“ allow-same-origin-origin”标志。 Check the JSFiddle to see the above JavaScript add the cookie to your browser. 检查JSFiddle以查看上面的JavaScript将cookie添加到浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM