I am trying to use JavaScript to create and delete cookies. The way I have it setup is that when the user clicks on the accept button I create a cookie. If they click on the decline button I remove it.
However I find each time I try to remove the cookie it still remains. Would anyone be able to advise on what I am doing wrong?
CREATE COOKIE:
//Create Cookie
function createCookie(name, value, expires, path, domain) {
var cookie = name + "=" + escape(value) + ";";
if (expires) {
// If it's a date
if(expires instanceof Date) {
// If it isn't a valid date
if (isNaN(expires.getTime()))
expires = new Date();
}
else
expires = new Date(new Date().getTime() + parseInt(expires) * 1000 * 60 * 60 * 24);
cookie += "expires=" + expires.toGMTString() + ";";
}
if (path)
cookie += "path=" + path + ";";
if (domain)
cookie += "domain=" + domain + ";";
document.cookie = cookie;
}
//Delete Cookie
function deleteCookie(name, path, domain) {
console.log('deleting ' + name + ' cookie...');
createCookie(name, "", -1, path, domain);
}
//Accept Button Click - CREATE COOKIE
$('#accept').click(function(e){
e.preventDefault();
console.log("You've accept our cookie policy!")
createCookie("MyCookie", "Yummy", 2, "/", ".xyz.com");
});
//Decline Button Click - DELETE COOKIE
$('#decline').click(function(e){
e.preventDefault();
console.log("You've declined our cookie policy!");
deleteCookie("myCookie", "/", ".xyz.com");
});