删除cookie功能无效

时间:2014-06-27 11:50:32

标签: javascript cookies

我想删除我的cookie,代码可以获取并设置cookie ,,,但删除cookie无法正常工作 请建议我回答

代码

<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>

2 个答案:

答案 0 :(得分: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。将其更改为name,这是传递给函数的参数,它应该可以工作。

还使用+来连接字符串,你还有一个额外的=,这也阻止了它的工作。

<小时/> 请查看 this Fiddle 以查看其实际效果。

答案 1 :(得分:0)

这是一个jsfiddle:http://jsfiddle.net/CCLdr/

设置您的Cookie到期时间如下:

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

甚至更好......

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);
}

请参阅此帖子:javascript - delete cookie