【问题标题】:How to set cookie using javascript in Internet Explorer如何在 Internet Explorer 中使用 javascript 设置 cookie
【发布时间】:2015-04-01 20:56:11
【问题描述】:

设置 cokie 在 FF 和 Chrome 中有效,但在 IE 中无效

function setCookie(key, value) {
            var expires = new Date();
            expires.setTime(expires.getTime() + (1 * 24 * 60 * 60 * 1000));
            document.cookie = key + '=' + value +';path=/'+ ';expires=' + expires.toUTCString();
        }



function checkCookie()
    {
    var newsletter=getCookie("newsletter5");
    if (newsletter!=null && newsletter!="")
      {      
      }
    else 
      {
      setCookie("newsletter5", 2000);
      timeMsg();
      }
    }

setCookie("newsletter5", 2000);

当我将安全保护降低到低于平均水平时,它可以工作,但它应该在安全设置上工作

【问题讨论】:

    标签: javascript internet-explorer cookies


    【解决方案1】:
        setCookie("name","value",expiryDate,"/");
    
    
    
    
        // cookie.js file
        var cookieToday = new Date(); 
        var expiryDate = new Date(cookieToday.getTime() + 
        (365 *86400000)); // a year
    
    /* 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";
    }
    

    【讨论】:

    • 我之前已经找到了这个解决方案,尝试过但不适合我
    【解决方案2】:

    我认为主要问题是将值设置为document.cookie 当我设置一些值并检查 IE 时没有分配。

    【讨论】:

      【解决方案3】:

      在浏览器中启用 cookie 和 JavaScript

      https://my.sph.harvard.edu/jsp/misc/java_cook.jsp

      【讨论】:

      • 这不是一个好的解决方案,因为我尝试在商店中设置弹出窗口。对 IE 用户来说是个问题。
      猜你喜欢
      • 1970-01-01
      • 2020-05-17
      • 1970-01-01
      • 2012-01-02
      • 2015-03-19
      • 1970-01-01
      • 2013-03-23
      • 2014-05-06
      • 2016-09-24
      相关资源
      最近更新 更多