document.cookie=“cookiename=cookievalue;
过期=星期一,12Jun2015:00:00:00;路径=/;”
我在 Internet Explorer 10 上运行此脚本,但它不在 2 个 IE 选项卡之间共享 cookie。但是当我删除“过期”属性时,它似乎可以工作:
document.cookie= "cookiename=cookievalue ;path=/;"
但我不想删除“过期”属性。那么如何解决这个问题呢?
2021 更新:如果您不需要向服务器传递信息,请使用本地存储或会话存储 https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API
我从 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";
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)