如何在IE 9中的js中设置Cookie?

时间:2012-11-17 15:53:30

标签: javascript cookies internet-explorer-9

我有这段代码:

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

这在IE中不起作用

如何解决?

0 个答案:

没有答案