1

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.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 (e.g., '/', '/mydir') If not specified, defaults to the current path of the current document location.

;domain=domain (e.g., '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 (e.g., 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)

1 Comment

you are correct. I am using the way you have mentioned, but the problem here is cookie is not getting deleted after using document.cookie = "name=;expires=Thu, 01 Jan 1970 00:00:00 GMT"; I am using document.cookie="name="+$scope.user.name+";expires="+now.toGMTString(); for setting the cookie.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.