Cookies can store, but not delete

51 Views Asked by At

I am working on an exercise to get familiar with the setting and deletion of cookies. Naturally, I have set it in a phony bakery.

I have tried to follow the Hamper model for cookies, and have been able to store them onto the console. But for some reason, every method that I have tried to delete them from the local server, through Javascript, has failed. I have tried Cookies.expire(), Cookies.set((key,null)).get(key), etc. None of them seem to work, even as I restart the server.

For your convenience, I have attached a Github repository below.

https://github.com/jdshatz/cookies2

1

There are 1 best solutions below

0
On

There is a function in confections2.js

document.getElementById('Order').on('click', function(){

Which is not correct syntax. Instead it should be

    document.getElementById('Order').onclick = function() {
  var name = document.getElementById('consumer_name').value;
    Cookies.set('consumer_name', name).get('consumer_name');
  var chocolate = document.getElementById('chocolate').value;
    Cookies.set('chocolate', chocolate).get('chocolate');
  var sugar = document.getElementById('sugar').value;
    Cookies.set('sugar', sugar).get('sugar');
  var lemon = document.getElementById('lemon').value;
    Cookies.set('lemon', lemon).get('lemon');
}

Then change in index.html

<input type="button" value="Reset" onclick="clearCookies();">

Then change the javascript function - to include reload as well as function name. It seems that clear() produces no results :(

function clearCookies(){ 
  Cookies.set('consumer_name', null);
  Cookies.set('chocolate', 0);
  Cookies.set('lemon',0);
  Cookies.set('sugar',0);
  location.reload();

};

It resets the values to null and 0. But it actually does not delete cookies. You can modify like this -

    function clearCookies(){ 
//      Cookies.set('consumer_name', null);
//      Cookies.set('chocolate', 0);
//      Cookies.set('lemon',0);
//      Cookies.set('sugar',0);
        delete_cookie('consumer_name');
        delete_cookie('chocolate');
        delete_cookie('lemon');
        delete_cookie('sugar');
      location.reload();
};


function delete_cookie(name) {
  document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}