javascript 使用javascript在IE中设置cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17966219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using javascript to set cookie in IE
提问by monocular
document.cookie= "cookiename=cookievalue; expires=Mon,12Jun2015:00:00:00; path=/;"
document.cookie= "cookiename=cookievalue; expires=Mon,12Jun2015:00:00:00; path=/;"
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 :
我在 Internet Explorer 10 上运行此脚本,但它不会在 2 个 IE 选项卡之间共享 cookie。但是当我删除“过期”属性时,它似乎可以正常工作:
document.cookie= "cookiename=cookievalue ;path=/;"
But i don't want to remove the "expires" properties. So how to resolve this problem ?
但我不想删除“过期”属性。那么如何解决这个问题呢?
回答by mplungjan
I have used this code since mid '90s - it has worked in all browsers on all platforms so far
我从 90 年代中期开始使用此代码 - 到目前为止,它已在所有平台上的所有浏览器中运行
Include the file and use
包含文件并使用
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";
}
回答by Sharad
The following sample code will demonstrate setting a cookie of your choosing directly, without requiring input from the user. To store a cookie from your site, simply put a call to the javascript function in your HTML page, like this:
以下示例代码将演示如何直接设置您选择的 cookie,而无需用户输入。要从您的站点存储 cookie,只需调用 HTML 页面中的 javascript 函数,如下所示:
<script type="text/javascript">cookieSet();</script>
The real work is done by the cookieSet() javascript function, which can be either in the area of your HTML page, or in a separate javascript file:
真正的工作是由 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";
}
}