【问题标题】:creating cookies on file:/// url - evidence it is being created but not showing in document.cookie or cookie list in chrome dev tools在 file:/// url 上创建 cookie - 证明它正在创建但未显示在 document.cookie 或 chrome 开发工具中的 cookie 列表中
【发布时间】:2017-11-21 16:03:58
【问题描述】:

我有一些创建 cookie 的代码。它创建了两个不同的用户组,检查用户 cookie 是什么组,并根据它做一些事情。

代码正在本地机器上开发,因此可以通过 file:/// URL 访问它

看起来像这样:

function cookieCreation() {
                var groupId = Math.floor(Math.random() * 2) + 1;
                var expDate = new Date(new Date().getTime()+60*60*1000*24).toGMTString()
                document.cookie = "userGroup_" + groupId + ";" + "expires=" + expDate +";path=/";
            }

            function getCookie(cname) {
                var name = cname + "=";
                var ca = document.cookie.split(';');
                    for(var i = 0; i < ca.length; i++) {
                        var c = ca[i];
                            while (c.charAt(0) == ' ') {
                                c = c.substring(1);
                            }
                        if (c.indexOf(name) == 0) {
                            return c.substring(name.length, c.length);
                        }
                    }
                return "";
            }

            function cookieBasedPush() {
                cookieCreation();
                var currentCookie = getCookie("userGroup_1");

                if (currentCookie == null) {
                    console.log("User Group 2");
                } else {
                    console.log("User Group 1");
                }
            }

            window.onload = cookieBasedPush;

一个 cookie 看起来像 它正在被创建,因为我可以看到“用户组 1”正在被控制台记录。但是,当我运行 document.cookie 时,我什么也得不到。

我搜索并找到Setting Cookies using JavaScript in a local html file 并基于此答案,建议无法在 file:/// URL 中创建 cookie。

这让我感到困惑并且没有任何意义 - 如果是这种情况,那么为什么我会成功获得用户组 1 cookie 创建的 console.log?为什么在控制台中运行时 document.cookie 为空白?我的逻辑是不是代码不对?

【问题讨论】:

    标签: javascript file cookies


    【解决方案1】:

    根据RFC 2109,Cookie 严格来说是一种 HTTP 机制。

    请参阅此链接here,了解有关此问题的 chrome“错误”报告。

    但是,您可以运行带有标志 --enable-file-cookies 的 chrome 以允许 file:/// cookies

    要运行带有实验/开发人员标志的 chrome,请查看 here 以获取说明

    似乎--enable-file-cookies 标志已从除 Android 之外的所有运行 chrome 的平台上删除。 你可以阅读更多关于它的信息herehere

    有了这个,似乎没有办法在file:/// URL结构下存储cookie,解决这个问题的最好方法是在开发时在本地运行一个小型服务器。这是一个很好的脚本列表,可以从命令行运行本地 HTTP 服务器。 link

    【讨论】:

    • 好的 - 请记住这一点,如果在 file:/// 上禁用了 cookie,为什么此代码控制台会记录“用户组 1”?我的逻辑缺陷是什么?
    • 您仍在获取日志"User Group 1",因为您正在检查函数getCookie()(即空字符串)中的返回值是否等于null,首先,您不需要在函数末尾添加return,其次,null 不等于""。看看这里了解数据类型developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures |此外,在检查某事物是否与某事物“相等”时,请确保使用类型安全的相等运算符 ===!== 而不是常规对应的 ==!=
    • 我很难得到它,所以我提前道歉。所以这意味着正在生成一个cookie,对吗?因为如果它为空,它将返回用户组 2(我意识到这是一个缺陷,但我稍后会尝试解决)我进行了更改,因此更改了检查 null 以检查 @987654343 中的 "" @ 函数,删除了 retrun 并使用类型安全运算符,所以 === 但我仍然得到用户组 1。
    • 看看我的答案,我已经更新它以纠正我之前关于使用上述标志运行 chrome 的错误。
    • 那时应该没有 cookie,并且应该始终为空。如果我得到用户组 1,它不能是 null,对吗?
    猜你喜欢
    • 2015-05-24
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 2013-01-04
    • 1970-01-01
    • 2023-03-18
    • 2022-08-23
    相关资源
    最近更新 更多