使用javascript在IE中设置cookie [英] Using javascript to set cookie in IE

查看:95
本文介绍了使用javascript在IE中设置cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


document.cookie =cookiename = cookievalue;
expires = Mon,12Jun2015:00:00:00; path = /;

document.cookie= "cookiename=cookievalue; expires=Mon,12Jun2015:00:00:00; path=/;"

我在我的Internet Explorer 10上运行此脚本,但它不在2 IE标签之间共享cookie。但当我删除过期属性,所以它似乎工作:

I run this script on my Internet Explorer 10 but it doesn't share cookie between 2 IE tab. But when i remove the "expires" properties so it seem to working :

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

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

But i don't want to remove the "expires" properties. So how to resolve this problem ?

推荐答案

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

I have used this code since mid '90s - it has worked in all browsers on all platforms so far

包含文件并使用

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

这篇关于使用javascript在IE中设置cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆