-2

I want to delete my cookies, code works upto get and set cookies,,, But deleting of cookie is not working Please suggest me answer

Code

<body>
    <select id="ThemeSelect" onchange="saveTheme(this.value);">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
        <option value="4">Four</option>
        <option value="5">Five</option>
    </select>
    <button type="button" onclick="ShowCookie();">GetCookie</button>
    <button type="button" onclick="deleteCookie(name);">DeleteCookies</button>

    <script>
        var saveclass = null;
        function saveTheme(cookieValue) {
            var sel = document.getElementById('ThemeSelect');

            saveclass = saveclass ? saveclass : document.body.className;
            document.body.className = saveclass + ' ' + sel.options[sel.value].innerHTML.replace(" ", "").toLowerCase();

            setCookie('theme', cookieValue, 365);
        }

        function setCookie(cookieName, cookieValue, nDays) {
            var today = new Date();
            var expire = new Date();

            if (nDays == null || nDays == 0)
                nDays = 1;

            expire.setTime(today.getTime() + 3600000 * 24 * nDays);
            document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString();

        }

        function getCookie(name) {
            var re = new RegExp(name + "=([^;]+)");
            var value = re.exec(document.cookie);
            return (value != null) ? unescape(value[1]) : null;

        }
        function ShowCookie() {
            var a = getCookie(name);
            alert(a);
        }

        function deleteCookie(name) {
            if (getCookie(name)) {
                var cookie_date = new Date();  // current date & time
                cookie_date.setTime(cookie_date.getTime() - 1);
                document.cookie = cookieName += "=; expires=" + cookie_date.toGMTString();
            }
        }

    </script>

</body>
Jot Dhaliwal
  • 1,470
  • 4
  • 26
  • 47
  • possible duplicate of [javascript - delete cookie](http://stackoverflow.com/questions/2144386/javascript-delete-cookie) – padarom Jun 27 '14 at 11:52
  • i have different code. it cant be duplicate @Padarom – Jot Dhaliwal Jun 27 '14 at 11:53
  • It's not about your code, it's about the question. There's a function in the linked thread that does exactly what you want. Therefore it's a duplicate. – padarom Jun 27 '14 at 11:55
  • i change the question name , please remove duplicate flag – Jot Dhaliwal Jun 27 '14 at 12:00
  • The code is in pretty bad shape. You're calling functions with undefined parameters, `document.cookie = cookieName += "=; expires="` -- `cookieName` is undefined, `+=` there doesn't do what you think it does, etc etc – JJJ Jun 27 '14 at 12:01
  • getcookie is giving cookies back – Jot Dhaliwal Jun 27 '14 at 12:03

2 Answers2

2
function deleteCookie(name) {
    if (getCookie(name)) {
        var cookie_date = new Date();  // current date & time
        cookie_date.setTime(cookie_date.getTime() - 1);
        document.cookie = name + "=; expires=" + cookie_date.toGMTString();
    }
}

cookieName is undefined in this scope. Change it to name, which is the parameter passed to the function and it should work.

Also use a + to concatenate strings, you had an additional = there which also prevented it from working.


Look at this Fiddle to see it in action.
padarom
  • 3,529
  • 5
  • 33
  • 57
  • 1
    Works fine for me: http://jsfiddle.net/DB2TQ/49/ (Open your console to see the values) – padarom Jun 27 '14 at 12:30
  • I don't know which changes you copied to your code prior to testing it, but I've just used the code I already posted in this answer. – padarom Jun 27 '14 at 12:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56422/discussion-between-jatt-net-born2code-and-padarom). – Jot Dhaliwal Jun 27 '14 at 12:51
0

Here's a jsfiddle: http://jsfiddle.net/CCLdr/

Set your cookies expiration like this:

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

or even better...

var createCookie, readCookie, eraseCookie;

createCookie = function(name,value,days) {
  var date, expires;

  if (days) {
      date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      expires = "; expires="+date.toUTCString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

readCookie = function(name) {
  var nameEQ, ca, i, c; 

  nameEQ = name + "=";
  ca = document.cookie.split(';');
  for(i=0;i < ca.length;i++) {
      c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

eraseCookie = function(name) {
  createCookie(name,"",-1);
}

see this post: javascript - delete cookie

Community
  • 1
  • 1
mdlars
  • 909
  • 10
  • 16
  • i have many other posts to see and many code, but i want to make my code perfect, please suggest better answer after make my code correct – Jot Dhaliwal Jun 27 '14 at 12:20
  • The methods I provided to you work just fine. There is no such thing as perfect code, it can only be measured in how maintainable and extendable it is for the future. You should not reference javascript methods on your HTML elements, you should instead apply functions to those elements through event handlers. P.S. who ever down voted is ridiculous. – mdlars Jun 27 '14 at 12:46