Jordan Carter
Jordan Carter

Reputation: 1336

How do I clear this JavaScript cookie?

I am trying to clear a cookie but am having difficulty. Basically, all I am trying to do is change the body background colour based on the value in the cookie. I can create the cookie fine, but my "clearCookie" function doesn't seem to be clearing it like I would expect it to.

function setTheme () {

    if (document.cookie) {
        document.cookie += "; max-age=0";
    }

    document.cookie = $('input:radio[name=theme]:checked').val() + "; max-age=86400; path=/";
    $('body').css('background', document.cookie);
}

function clearCookie () {

    if (document.cookie) {
        document.cookie += "; max-age=0";
        $('body').css('background', '#F2F2F2');
    }

}

$(document).ready(function () {

    if (document.cookie != "") {
        $('body').css('background', document.cookie);
    }

    $('#theme').click(setTheme);
    $('#default').click(clearCookie);

});

Upvotes: 0

Views: 183

Answers (1)

trincot
trincot

Reputation: 349926

You should add the path also when clearing:

document.cookie += "; max-age=0; path=/";

Upvotes: 1

Related Questions