繁体   English   中英

document.cookie在IE中不起作用

[英]document.cookie not working in IE

此示例在Firefox,Opera和Chrome中正常运行,但无法正常工作/ Cookie无法保存在IE中。

function setCookie(name,value,exdays)
    {
        var exdate=new Date();
        exdate.setDate(exdate.getDate() + exdays);
        var value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
        document.cookie=name + "=" + value;
    }

    function getCookie(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==name)
            {
            return unescape(y);
            }
          }
    }

IE与所有其他浏览器不同,在编码方面存在问题,某些语言不会自动编码,而是%3F编码。

“%3F” =“?” 标志。

您需要在IE上查看cookie并检查其内容,检查文件是否正确编码。 如果没有,请像这样使用它:

 document.cookie=name + "=" + (encodeUriComponent)value;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM