1

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:

  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?

Taras Kovalenko
  • 2,323
  • 3
  • 24
  • 49
  • 1
    possible duplicate of [How do I create and read a value from cookie?](http://stackoverflow.com/questions/4825683/how-do-i-create-and-read-a-value-from-cookie) – FrancoisBaveye May 13 '15 at 13:33

3 Answers3

1

Check this checkbox in browser settings: http://browsers.about.com/od/internetexplorertutorials/ss/ie8privatedata_8.htm

enter image description here

IonDen
  • 773
  • 5
  • 15
1

I found good code for get/set cookies:

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?

Thanks you heru-luin

Community
  • 1
  • 1
Taras Kovalenko
  • 2,323
  • 3
  • 24
  • 49
1

See the official MS Developer Network docs -> 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.

So you basically need to specify an expiration date if you want the cookie to persist in IE. Example from the link above :

// 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.

Community
  • 1
  • 1
davidkonrad
  • 83,997
  • 17
  • 205
  • 265