0

I have created javascript functions to set and delete cookies.

setCookie is working perfectly but eraseCookie is not working.

HTML

<a onclick="setCookie('analystics');">Allow</a>
<a onclick="eraseCookie('analystics')">Deny</a>
<p>Click on this paragraph.</p>

JavaScript

function setCookie(cname,cvalue,exdays) {
     var d = new Date();
     d.setTime(d.getTime() + (30 * 1000));
     var expires = "expires=" + d.toGMTString();
     document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
     var values = document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
     doSomething(cname);
}

function doSomething(cname) {
    console.log(cname);
    var myCookie = cname;
    if (myCookie == null) {
        alert('null');
    } else {
        alert('defined');
        $("p").click(function() {
            alert("The paragraph was clicked.");
        });
    }
}

Cookie Delete Function:

function eraseCookie(cname) {
    setCookie(cname, '', -1);
    alert('The cookie has been successfully erased.');
}
PRMoureu
  • 12,817
  • 6
  • 38
  • 48
  • 1
    Possible duplicate of [How to delete a cookie?](https://stackoverflow.com/questions/2144386/how-to-delete-a-cookie) – spectacularbob Apr 17 '18 at 16:27
  • Set the cookie to expire at some date in the past, e.g. `expires=Thu, 01 Jan 1970 00:00:00 GMT` – j08691 Apr 17 '18 at 16:28
  • If `****Cokkie Delete Function:****` is really in your javascript file that will brake the code on that line cause `****` is not to use for comments. Use `//` instead. – caramba Apr 17 '18 at 16:30
  • I think you are reinventing the wheel here. Take a look at this: https://github.com/js-cookie/js-cookie – GG. Apr 17 '18 at 16:44

0 Answers0