3

I want to use one cookie for all my pages and I want to update this cookie for all pages also. It will be used in layout as a theme value.

I have created a cookie named as theme by using javascript method;

$.cookie("theme", "skin-blue");

And checked the cookie whether is saved or not by using;

document.cookie "theme=skin-blue"

When i wanna delete the cookie by using it returns false;

$.removeCookie("theme") false

Can you help me to solve the issue? Is there anyway to delete or clear or cookies? Thanks.

Note: This method works for one page but not for all pages. In other pages it creates new cookie as same name so i cannot update the related one.

3
  • What did you think $.removeCookie("theme") false was going to do? Commented Jan 13, 2015 at 7:57
  • Is is possible to provide a plunker to reproduce your issue? Commented Jan 13, 2015 at 7:58
  • @jfriend00 i thought it should turn true and delete the cookie. Commented Jan 13, 2015 at 8:03

2 Answers 2

2

The standard way of deleting a cookie in JavaScript is to overwrite cookie's value to empty value and expires date to a passed date.

Example: Let's say that cookie you'd like to delete is called access_token. Below line should deleted the cookie, if cookie's domain is in the same domain, then this should work. We assume that access_token cookie is in example.com domain.

// Deleting access_token cookie with implicit domain.
document.cookie = "access_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC;"

However, If cookie's domain is in a different domain or a sub-domain above line should not work. Let me be more precise. This time, we assume that access_token cookie is in .example.com sub-domain (notice the dot in front of example.com). Then, above line should not work, thus cookie will not be deleted.

Solution, for cases where cookie is in a different domain/sub-domain, then you have to explicitly specify domain/sub-domain name while deleting cookie.

// Deleting access_token cookie, with explicit domain/sub-domain
document.cookie = "access_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; domain=.example.com/"
Sign up to request clarification or add additional context in comments.

Comments

0

You could set the cookies date to expired, something like:

function removeCookie (name) {
    document.cookie = name + '=;expires=Mon, 01 Mar 1980 00:00:01 GMT;';
}

so just doing removeCookie("theme");

would remove your theme cookie

6 Comments

Thanks,yes it is useful but what about deleting forever :)
@Cracker Don't know about you, but for me, it is gone forever when setting to null
If you do $.cookie("theme", "skin-blue") and then type document.cookie you will see it, if you type $.cookie("theme", null); and then type document.cookie it is gone, is it not?
@Cracker Hmm.. Weird, on my end it doesn't exist anymore using Chrome / Chrome dev tools, that's how I've always done it
really really weird anyway thanks for your advise. I try to figure out.
|

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.