0

I have this code:

<input type='button' onclick='window.setCookie("userId", 123); alert(window.getCookie("userId"))' value='dsa'></input>

var    userId = 999;
window.setCookie = function(c_name, value)
    {
      exdays = 30;
      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;
    }

window.getCookie = function(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);
          }
        }
    }

http://jsfiddle.net/aucja/

And this is not working in IE

how to fix it?

Tom Smykowski
  • 25,487
  • 54
  • 159
  • 236
  • 2
    "Not working" how? The cookie isn't set? Are there any errors? What have you tried in terms of debugging? Also, you should use `max-age` instead of `expires`; it saves the date thing, too. – Ry- Nov 17 '12 at 15:55
  • +minitech i Provided link to jsfiddle you can see how does it work.After calling the method to set cookie i read it and i get undefined... – Tom Smykowski Nov 17 '12 at 15:56
  • So what's in `document.cookie` after calling it? Debug! – Ry- Nov 17 '12 at 15:58
  • 1
    Did you check your browser settings? – Shashank Kadne Nov 17 '12 at 15:59
  • @minitech in document.cookie i have: "window_sizes=%7B%22w%22%3A%5B%2250%25%22%2C%2250%25%22%5D%2C%22h%22%3A%5B%2225%25%22%2C%2275%25%22%2C%2225%25%22%2C%2275%25%22%5D%7D; window_sizes=%7B%22w%22%3A%5B%2250%25%22%2C%2250%25%22%5D%2C%22h%22%3A%5B%2225%25%22%2C%2275%25%22%2C%2225%25%22%2C%2275%25%22%5D%7D; __utma=210580238.38040181.1333754837.1333754837.1353167273.2; __utmz=210580238.1353167273.2.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmb=210580238.4.10.1353167273; __utmc=210580238" – Tom Smykowski Nov 17 '12 at 16:03
  • @tomaszs: Check it on your page, not on the jsFiddle. Those are its cookies. – Ry- Nov 17 '12 at 16:05
  • @ShashankKadne When i set "privacy" to low or "accept all cookies" it works fine. But in the default setting "medium" it does not work. How to make it work in "medium" setting? – Tom Smykowski Nov 17 '12 at 16:07
  • @minitech on my website i have: __utma=86865561.1294741938.13242091241.1350201795.1353168511.6; __utmz=86865561.13224091241.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); fusion2_visited=yes; __cfduid=d4f30d1aa325a55b9d19dd41dc30e54e2a1340698134; __qca=P0-12080787147-1341919977192; 04ffc1916de0a4c86cf85827af24a4c28=4436c6f170b56b3b75df402dcaf0ac89; __utmb=86865561.1.10.13453168511; __utmc=86865456 – Tom Smykowski Nov 17 '12 at 16:08
  • nvmd, i've found the solution: http://stackoverflow.com/questions/6422164/cookie-not-being-sent-in-iframe-in-ie9 vote to close – Tom Smykowski Nov 17 '12 at 16:18
  • Well if you found a solution may be you can answer your own question. – Shashank Kadne Nov 17 '12 at 16:20
  • @ShashankKadne Well its a duplicate i suppose – Tom Smykowski Nov 17 '12 at 23:24

0 Answers0