【问题标题】:javascript delete cookie works bizarre across browserjavascript删除cookie在浏览器中的工作很奇怪
【发布时间】:2013-02-02 17:12:45
【问题描述】:

我正在尝试对 javascript cookie 进行演示测试。请在下面找到我为测试编写的代码。

<html>
<head>
<script type='text/javascript' >

function setcookie()
{   
    alert("check if cookie avail:" +document.cookie.split(';'));
    var dt=new Date();

    document.cookie='name=test';
    document.cookie='expires='+dt.toUTCString()+';'
    alert("now cookie val:" +document.cookie.split(';'));

    dt.setDate(dt.getDate()-1);
    document.cookie = "expires=" + dt.toUTCString() + ";"
    alert("after deletion cookie val:" + document.cookie.split(';'));
 }
</script>

</head>
<body>
    <input id='txt' onchange='setcookie()' />
</body>
</html>

代码将作为,

最初,这将显示该浏览器中已经存在的 cookie,然后我尝试将 cookie 设置为具有 1 天过期时间的 'name=test'。使用警报我可以看到该 cookie 中设置的值。在下一行中,我尝试通过将过期日期设置为当前 date-1 来删除 cookie。如果我使用 alert 来打印 cookie 值,cookie 将显示为 currentdate-1 的过期日期。

我的问题是,

  1. 在 Mozilla 中,如果我刷新浏览器并尝试执行相同的步骤,则第一个警报会显示 cookie 值,其过期时间为 currentdate-1。为什么即使我在脚本的最后一行删除,我也会获得 cookie 值。但是,一旦我关闭浏览器,cookie 值为空。为什么会这样?
  2. 在 chrome 中,如果我运行同一段代码,则不会设置任何 cookie。为什么我无法在 chrome 浏览器中设置 cookie。

请告诉我为什么跨浏览器会出现这种差异。

【问题讨论】:

    标签: javascript cookies


    【解决方案1】:

    这不是设置过期时间

    document.cookie='name=test';
    document.cookie='expires='+dt.toUTCString()+';'
    

    这是

    document.cookie='name=test; expires='+dt.toUTCString()+';'
    

    最好的办法是获取经过良好测试的 cookie 代码并使用它

    如果你使用 jQuery,试试这个或者使用 jQuery 插件

    // cookie.js file
    var daysToKeep = 14; // default cookie life...
    var today      = new Date(); 
    var expiryDate = new Date(today.getTime() + (daysToKeep * 86400000));
    
    
    /* Cookie functions originally by Bill Dortsch */
    function setCookie (name,value,expires,path,theDomain,secure) { 
       value = escape(value);
       var theCookie = name + "=" + value + 
       ((expires)    ? "; expires=" + expires.toGMTString() : "") + 
       ((path)       ? "; path="    + path   : "") + 
       ((theDomain)  ? "; domain="  + theDomain : "") + 
       ((secure)     ? "; secure"            : ""); 
       document.cookie = theCookie;
    } 
    
    function getCookie(Name) { 
       var search = Name + "=" 
       if (document.cookie.length > 0) { // if there are any cookies 
          var offset = document.cookie.indexOf(search) 
          if (offset != -1) { // if cookie exists 
             offset += search.length 
             // set index of beginning of value 
             var end = document.cookie.indexOf(";", offset) 
             // set index of end of cookie value 
             if (end == -1) end = document.cookie.length 
             return unescape(document.cookie.substring(offset, end)) 
          } 
       } 
    } 
    function delCookie(name,path,domain) {
       if (getCookie(name)) document.cookie = name + "=" +
          ((path)   ? ";path="   + path   : "") +
          ((domain) ? ";domain=" + domain : "") +
          ";expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
    

    【讨论】:

    • 抱歉,我不想要任何现有代码。在这里,我尝试通过做示例来自己学习 javascript 代码。我只是想知道我的代码在浏览器中无法正常工作的原因。
    猜你喜欢
    • 2013-07-22
    • 1970-01-01
    • 2016-10-08
    • 2010-11-19
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多