Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using javascript to set cookie in IE

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 :

document.cookie= "cookiename=cookievalue ;path=/;" 

But i don't want to remove the "expires" properties. So how to resolve this problem ?

like image 601
monocular Avatar asked Jul 31 '13 09:07

monocular


1 Answers

2021 update: If you do NOT need to pass information to the server, use localStorage or sessionStorage

I have used this code since mid '90s - it has worked in all browsers on all platforms so far

Include the file and use

setCookie("name","value",expiryDate,"/"); // the last two are optional

// 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";
}
like image 104
mplungjan Avatar answered Sep 24 '22 05:09

mplungjan