IE关闭浏览器后退出

IE doing logout after close the browser

我创建网站,当用户登录并检查时 "remember me" 我写入 cookie 用户名。它运行良好,但仅适用于某些浏览器。 我在 cookie 用户名中写入的代码:

  document.cookie = "";
  document.cookie = "username=" + username;

登录后,我从 cookie 中检查用户名。 但在 IE 浏览器中它不起作用。 关闭浏览器并再次打开他后,饼干正在清除。 为什么会这样? 以及如何修复它?

在浏览器设置中选中此复选框:http://browsers.about.com/od/internetexplorertutorials/ss/ie8privatedata_8.htm

我找到了 get/set 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);
      }
     }
    }

来源:How do I create and read a value from cookie?

谢谢你heru-luin

查看官方 MS 开发者网络文档 -> 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 在 IE 中持续存在,您基本上需要指定一个过期日期。上面 link 的示例:

// 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()); 
}

或者看到这个优秀的答案 -> Using javascript to set cookie in IE