简体   繁体   中英

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. I used the below code for setting cookie value using javascript -

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 -

document.cookie = "name= ;email= ;expires=Thu, 01 Jan 1970 00:00:00 GMT";

but nothing seems to work. the cookie is still present. I tried setting the cookie this way also -

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. 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. I checked the results on chromium

Version 50.0.2661.102 Ubuntu 16.04 (64-bit)

and on opera

Version: 37.0.2178.32

in both cases cookie is not getting deleted. One more information is I am including these two codes in two different API calls.

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. 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. For example your 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(); is actually creating a cookie with the name expires , not setting an expiry time.

When you do 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.

Check https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie for proper usage. But in summary the valid parameters for setting a cookie is as follows:

;path=path (eg, '/', '/mydir') If not specified, defaults to the current path of the current document location.

;domain=domain (eg, 'example.com' or '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)

;expires=date-in-GMTString-format If not specified it will expire at the end of session.

;secure (cookie to only be transmitted over secure protocol as https)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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