[英]cookie not getting deleted using javascript
I have searched various articles and links for deleting cookie using javascipt, but it seems the javascript not working. 我搜索了各种文章和链接,以使用javascipt删除cookie,但似乎javascript无法正常工作。 I used the below code for setting cookie value using javascript -
我使用以下代码使用javascript设置cookie值-
var now = new Date();
var time = now.getTime();
time += 3600 * 1000;
now.setTime(time);
document.cookie="name=" + $scope.user.name;
document.cookie="email=" + $scope.user.email;
document.cookie ="expires=" + now.toGMTString();
and then while trying to remove the cookie used the below code as in the link w3 schools - 然后在尝试删除Cookie时,使用以下代码,如链接w3学校一样 -
document.cookie = "name= ;email= ;expires=Thu, 01 Jan 1970 00:00:00 GMT";
but nothing seems to work. 但似乎没有任何效果。 the cookie is still present.
Cookie仍然存在。 I tried setting the cookie this way also -
我也尝试过以这种方式设置Cookie-
document.cookie="name=" + $scope.user.name+";email=" + $scope.user.email+";expires=" + now.toGMTString();
and then again used the same delete operation but cookie iis not getting deleted. 然后再次使用相同的删除操作,但cookie不会被删除。 What is the problem.
问题是什么。 I can see that both ways of assigning cookie value is different but the cookie shouls be deleted which is not happening.
我可以看到,分配cookie值的两种方法都不同,但是cookie应该被删除,这是没有发生的。 I checked the results on chromium
我检查了铬的结果
Version 50.0.2661.102 Ubuntu 16.04 (64-bit)
版本50.0.2661.102 Ubuntu 16.04(64位)
and on opera 和歌剧
Version: 37.0.2178.32
版本:37.0.2178.32
in both cases cookie is not getting deleted. 在两种情况下,cookie都不会被删除。 One more information is I am including these two codes in two different API calls.
另外一个信息是我将这两个代码包含在两个不同的API调用中。
There seems to be some problem which I am not able to figure it out. 似乎有些问题我无法解决。 But, if add 'path=/' , then the cookie seems to be created and get deleted without any issue.
但是,如果添加'path = /',则cookie似乎已创建并被删除,没有任何问题。 the code for the same is as below for creation and deletion.
相同的代码如下所示,用于创建和删除。
document.cookie="name="+$scope.user.name+";expires="+now.toGMTString()+";path=/";
document.cookie = "name=; expires=Thu, 18 Dec 2013 12:00:00 GMT; path=/";
Thanks for the help. 谢谢您的帮助。
Every time you do a document.cookie
assignment, you're creating a new, separate cookie. 每次执行
document.cookie
分配时,您都在创建一个新的单独的cookie。 For example your document.cookie="name=" + $scope.user.name;
例如,您的
document.cookie="name=" + $scope.user.name;
only sets the name
and does not actually set an expires
because you didn't provide the paramter, and your document.cookie ="expires=" + now.toGMTString();
只设置
name
,实际上不设置expires
因为您没有提供参数和document.cookie ="expires=" + now.toGMTString();
is actually creating a cookie with the name expires
, not setting an expiry time. 实际上是使用名称
expires
创建cookie,而不设置过期时间。
When you do document.cookie = "name= ;email= ;expires=Thu, 01 Jan 1970 00:00:00 GMT";
当您执行
document.cookie = "name= ;email= ;expires=Thu, 01 Jan 1970 00:00:00 GMT";
however, that should cause the name
cookie to expire, but leaving the email
cookie there because the email=
parameter is not a valid parameter for setting a cookie. 但是,这将导致
name
cookie过期,但将email
cookie保留在那里,因为email=
参数不是用于设置cookie的有效参数。
Check https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie for proper usage. 检查https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie以确保正确使用。 But in summary the valid parameters for setting a cookie is as follows:
但总而言之,用于设置Cookie的有效参数如下:
;path=path
(eg, '/', '/mydir') If not specified, defaults to the current path of the current document location.;path=path
(例如'/','/ mydir')如果未指定,则默认为当前文档位置的当前路径。
;domain=domain
(eg, 'example.com' or 'subdomain.example.com').;domain=domain
(例如,“ example.com”或“ subdomain.example.com”)。 If not specified, defaults to the host portion of the current document location (but not including subdomains).如果未指定,则默认为当前文档位置的主机部分(但不包括子域)。
;max-age=max-age-in-seconds
(eg, 60*60*24*365 or 31536e3 for a year);max-age=max-age-in-seconds
(例如一年的60 * 60 * 24 * 365或31536e3)
;expires=date-in-GMTString-format
If not specified it will expire at the end of session.;expires=date-in-GMTString-format
如果未指定,它将在会话结束时过期。
;secure
(cookie to only be transmitted over secure protocol as https);secure
(cookie仅通过安全协议以https传输)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.