10

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 ?

NREZ
  • 942
  • 9
  • 13
monocular
  • 315
  • 1
  • 6
  • 18
  • 2
    What do you mean by doesn't share cookie between 2 IE tab. As long as it doesn't expire; cookie will be available to all tabs of all instances of that particular browser (in your case IE-10). – Prash Jul 31 '13 at 09:14
  • @Prash I mean i run the set cookie code from tab 1, but when i switch to tab 2, i run the alert(document.cookie) and doesn't see the cookie i set from tab 1 – monocular Jul 31 '13 at 09:28
  • You haven't provided enough information. What sites are tab #1 and #2 located on? If you click "View > Webpage Privacy Policy" in IE after trying to set the cookie, do you find that your persistent cookie was "Blocked" or "Leashed"? – EricLaw Jul 31 '13 at 21:00

2 Answers2

25

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";
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

The following sample code will demonstrate setting a cookie of your choosing directly, without requiring input from the user. To store a cookie from your site, simply put a call to the javascript function in your HTML page, like this:

<script type="text/javascript">cookieSet();</script>

The real work is done by the cookieSet() javascript function, which can be either in the area of your HTML page, or in a separate javascript file:

var cookieText = "Put your desired cookie value here";
var cookiePrefix = "";
var myPage = location.href;
var wwwFlag = myPage.indexOf('www');
if (wwwFlag > 0) {
cookiePrefix = "www";
}
var cookieName = cookiePrefix + "cbCookie";
function cookieSet() {
if (document.cookie != document.cookie) {
index = document.cookie.indexOf(cookieName);
} else {
index = -1;
}
if (index == -1) {
document.cookie=cookieName+"="+cookieText+"cbEndCookie; expires=Monday, 04-Apr-2020 05:00:00 GMT";
}
}
Sharad
  • 743
  • 2
  • 14
  • 23
  • It doesn't work. I run it in the first IE tab, but in another IE tab i run the code alert(document.cookie); and doesn't see the cookie from tab 1 – monocular Jul 31 '13 at 09:27
  • @monocular that's perfectly normal if you did not reload tab2 in between. – Kir Kanos Nov 12 '14 at 16:59