I’m trying to use Javascript to update some cookies for a website. It should look to see if an existing cookie as a certain value. If so, delete the cookie and replace it.
Instead, it’s adding the new cookie without removing the original.
Here’s my code
$(document).ready(function(){
if(getCookie('ref') == 'na') {
$.cookie('ref', null, { path: '/', expires: -5 });
$.cookie('ref', Base64.encode(document.referrer), { expires: 365 });
}
});
Here’s the cookie library I’m using: https://github.com/carhartl/jquery-cookie
What am I doing wrong?
To delete a cookie, you must set it with the exact same path and domain you intend to delete it from. Specify the
pathin both$.cookie()calls, and if thedomainwas specified in any previous code, the domain must be specified as an exact match in the jQuery code.However, there is no real need to delete the cookie if you just intend to overwrite it: