Cookie是在Chrome中删除的,但不是在Firefox中删除的吗? [英] Cookie deletes in Chrome, but not Firefox?

查看:42
本文介绍了Cookie是在Chrome中删除的,但不是在Firefox中删除的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用HTML,PHP和JS编码购物车.我有一个 onclick 函数,该函数删除cookie并刷新页面,从而从购物车中删除该项目.这在Chrome浏览器中有效,并且Cookie被删除.我在Firefox中具有相同的代码,但是它不起作用:cookie不会在Firefox中删除.这是具有 onclick 属性的元素:

I'm coding a shopping cart in HTML, PHP, and JS. I have an onclick function that deletes a cookie and refreshes the page, which removes the item from the shopping cart. This works in Chrome and the cookie is deleted. I have the same code in Firefox, but it doesn't work: the cookie isn't deleted in Firefox. Here's the element with the onclick attribute:

echo'< button class ="button" onclick ="removeCart'.$ x.'()">从购物车中删除</button>';

功能如下:

                echo '<script>';
                echo 'function removeCart' . $x . '() {';
                echo 'document.cookie = "' . $itemsSpaced[$x] . '= ; expires=Thu, 01 Jan 1970 00:00:00 UTC";';
                echo ' location.reload(true);';
                echo '}';
                echo '</script>';

为什么这在Chrome浏览器中有效,但在Firefox中不起作用?

Why does this work in Chrome, but not Firefox?

推荐答案

在删除Cookie时,应确保添加与删除cookie相同的 path domain 属性(如果您指定了其中任何一项).这是因为这两个属性决定了cookie的可访问性.

You should make sure to add the same path and domain attributes in the deletion of the cookie as in its creation (if you specified any of these). This is because these two attributes determine the accessibility of the cookie.

例如,您可以有两个具有相同名称但链接到不同路径的cookie.如果您不指定路径,那么删除哪一个将是模棱两可的.显然,Firefox在这方面比Chrome更好.如果没有路径说明,则不应删除Cookie.

You could for instance have two cookies with the same name, but linked with a different path. It would be ambiguous which one to delete if you would not specify the path. Apparently Firefox deals better with this than Chrome. The cookie should not be deleted without the path specification.

所以您应该将代码更改为:

So you should probably change your code to:

echo 'document.cookie = "' . $itemsSpaced[$x] . '= ; path=/cart; expires=Thu, 01 Jan 1970 00:00:00 UTC";';

这篇关于Cookie是在Chrome中删除的,但不是在Firefox中删除的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆