I craete web-sites and when user login and check "remember me" I write to cookies username. It is working good, but just in some browsers. My code for write in cookies username:
我喜欢网站,当用户登录并检查“记住我”时,我写了cookie用户名。它工作得很好,但只是在某些浏览器中。用于写入cookie用户名的代码:
document.cookie = "";
document.cookie = "username=" + username;
And after login i check username from cookies. But in IE browser it is not working. After close the browser and open him again cookies doing clears. Why it is happend? And how to fix it?
登录后我检查cookie的用户名。但在IE浏览器中,它无法正常工作。关闭浏览器并再次打开他做饼干清除。为什么会这样?以及如何解决它?
3 个解决方案
#1
I found good code for get/set cookies:
我找到了获取/设置cookie的好代码:
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) +
((exdays==null) ? "" : ("; expires="+exdate.toUTCString()));
document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
Source:How do I create and read a value from cookie?
来源:如何从cookie创建和读取值?
Thanks you heru-luin
谢谢你heru-luin
#2
Check this checkbox in browser settings: http://browsers.about.com/od/internetexplorertutorials/ss/ie8privatedata_8.htm
在浏览器设置中选中此复选框:http://browsers.about.com/od/internetexplorertutorials/ss/ie8privatedata_8.htm
#3
See the official MS Developer Network docs -> https://msdn.microsoft.com/en-us/library/ms533693%28v=vs.85%29.aspx
查看官方MS Developer Network文档 - > https://msdn.microsoft.com/en-us/library/ms533693%28v=vs.85%29.aspx
If you set no expiration date on a cookie, it expires when the browser closes. If you set an expiration date, the cookie is saved across browser sessions. If you set an expiration date in the past, the cookie is deleted. Use Greenwich Mean Time (GMT) format to specify the date.
如果您没有在cookie上设置过期日期,它将在浏览器关闭时到期。如果您设置了过期日期,则会在浏览器会话中保存Cookie。如果您过去设置了过期日期,则会删除Cookie。使用格林威治标准时间(GMT)格式指定日期。
So you basically need to specify an expiration date if you want the cookie to persist in IE. Example from the link above :
因此,如果您希望cookie在IE中持久存在,您基本上需要指定过期日期。上面的链接示例:
// Create a cookie with the specified name and value.
function SetCookie(sName, sValue)
{
document.cookie = sName + "=" + escape(sValue);
// Expires the cookie in one month
var date = new Date();
date.setMonth(date.getMonth()+1);
document.cookie += ("; expires=" + date.toUTCString());
}
Or see this excellent answer -> Using javascript to set cookie in IE.
或者看到这个优秀的答案 - >使用javascript在IE中设置cookie。
#1
I found good code for get/set cookies:
我找到了获取/设置cookie的好代码:
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) +
((exdays==null) ? "" : ("; expires="+exdate.toUTCString()));
document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
Source:How do I create and read a value from cookie?
来源:如何从cookie创建和读取值?
Thanks you heru-luin
谢谢你heru-luin
#2
Check this checkbox in browser settings: http://browsers.about.com/od/internetexplorertutorials/ss/ie8privatedata_8.htm
在浏览器设置中选中此复选框:http://browsers.about.com/od/internetexplorertutorials/ss/ie8privatedata_8.htm
#3
See the official MS Developer Network docs -> https://msdn.microsoft.com/en-us/library/ms533693%28v=vs.85%29.aspx
查看官方MS Developer Network文档 - > https://msdn.microsoft.com/en-us/library/ms533693%28v=vs.85%29.aspx
If you set no expiration date on a cookie, it expires when the browser closes. If you set an expiration date, the cookie is saved across browser sessions. If you set an expiration date in the past, the cookie is deleted. Use Greenwich Mean Time (GMT) format to specify the date.
如果您没有在cookie上设置过期日期,它将在浏览器关闭时到期。如果您设置了过期日期,则会在浏览器会话中保存Cookie。如果您过去设置了过期日期,则会删除Cookie。使用格林威治标准时间(GMT)格式指定日期。
So you basically need to specify an expiration date if you want the cookie to persist in IE. Example from the link above :
因此,如果您希望cookie在IE中持久存在,您基本上需要指定过期日期。上面的链接示例:
// Create a cookie with the specified name and value.
function SetCookie(sName, sValue)
{
document.cookie = sName + "=" + escape(sValue);
// Expires the cookie in one month
var date = new Date();
date.setMonth(date.getMonth()+1);
document.cookie += ("; expires=" + date.toUTCString());
}
Or see this excellent answer -> Using javascript to set cookie in IE.
或者看到这个优秀的答案 - >使用javascript在IE中设置cookie。