【问题标题】:Using javascript to set cookie in IE在 IE 中使用 javascript 设置 cookie
【发布时间】:2013-07-31 17:35:25
【问题描述】:

document.cookie= "cookiename=cookievalue; 过期=周一,2015 年 6 月 12 日:00:00:00;路径=/;"

我在 Internet Explorer 10 上运行此脚本,但它不会在 2 个 IE 选项卡之间共享 cookie。但是当我删除“过期”属性时,它似乎可以工作:

document.cookie= "cookiename=cookievalue ;path=/;" 

但我不想删除“过期”属性。那么如何解决这个问题呢?

【问题讨论】:

  • 2 个 IE 标签页之间不共享 cookie 是什么意思。只要没有过期; cookie 将可用于该特定浏览器的所有实例的所有选项卡(在您的情况下为 IE-10)。
  • @Prash 我的意思是我从选项卡 1 运行设置的 cookie 代码,但是当我切换到选项卡 2 时,我运行警报(document.cookie)并且看不到我从选项卡设置的 cookie 1
  • 您没有提供足够的信息。标签 #1 和 #2 位于哪些站点?如果您在尝试设置 cookie 后在 IE 中点击“查看 > 网页隐私政策”,您是否发现您的持久性 cookie 被“阻止”或“束缚”?

标签: javascript internet-explorer internet-explorer-10


【解决方案1】:

2021 更新:如果您不需要向服务器传递信息,请使用localStorage or sessionStorage

我从 90 年代中期开始使用此代码 - 到目前为止,它已在所有平台上的所有浏览器中运行

包含文件并使用

setCookie("name","value",expiryDate,"/"); // the last two are optional

// 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】:

    以下示例代码将演示直接设置您选择的 cookie,无需用户输入。要从您的站点存储 cookie,只需调用 HTML 页面中的 javascript 函数,如下所示:

    <script type="text/javascript">cookieSet();</script>
    

    真正的工作是由 cookieSet() javascript 函数完成的,它可以在您的 HTML 页面区域中,也可以在单独的 javascript 文件中:

    var cookieText = "Put your desired cookie value here";
    var cookiePrefix = "";
    var myPage = location.href;
    var wwwFlag = myPage.indexOf('www');
    if (wwwFlag > 0) {
    cookiePrefix = "www";
    }
    var cookieName = cookiePrefix + "cbCookie";
    function cookieSet() {
    if (document.cookie != document.cookie) {
    index = document.cookie.indexOf(cookieName);
    } else {
    index = -1;
    }
    if (index == -1) {
    document.cookie=cookieName+"="+cookieText+"cbEndCookie; expires=Monday, 04-Apr-2020 05:00:00 GMT";
    }
    }
    

    【讨论】:

    • 它不起作用。我在第一个 IE 选项卡中运行它,但在另一个 IE 选项卡中我运行代码 alert(document.cookie);并且看不到标签 1 中的 cookie
    • @monocular 如果您没有在两者之间重新加载 tab2,那是完全正常的。
    猜你喜欢
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多