0

I am looking into an issue with javscript session cookies.

The situation is this, we have a main browser window, this creates a cookie, the initial value is an empty string.

document.cookie = "MyCookie_<%=UserID%>=" + value;

When a new window is opened (CTRL+N), this checks for the cookie, sees it has no value and then generates an id for the value, so we would have MyCookie_SomeUserID=12345 or whatever.

The issue we seem to have is that the original window when we do something like

var cookiestr = document.cookie;
var name = "MyCookie_<%=UserID%>";
var cookie = cookiestr.split(";");

for(var i = 0; i < cookie.length; i++)  {
  var cookiePart = cookie[i].split("=");
  cookiePart[0] = cookiePart[0].replace(/^\s+|\s+$/g, '');

  if(cookiePart[0] == name) {
    if(cookiePart.length > 1)  {
      return cookiePart[1];
    }
  }
}

if we split on the "=" we don't have an id it is the original blank string that this browser window used when creating the cookie, so cookiePart[1] always = "" it's like it can share the cookie with the session for the new window, but the new window can't share it's session cookie back with the original window.

Has anyone experienced/got around this?

  • 1
    do you need to use cookies? you could use the [localStorage](http://stackoverflow.com/questions/2236828/javascript-communication-between-tabs-windows-with-same-origin/12514384#12514384). If so, [this post](http://stackoverflow.com/questions/4079280/javascript-communication-between-browser-tabs-windows) may help – Pete Apr 28 '16 at 14:47
  • That may be a possibility. Thanks for that. –  Apr 28 '16 at 15:11

0 Answers0