在Android浏览器中禁用cookie无法正常工作

eag*_*e12 6 javascript cookies android mobile-safari

我正在运行Android Honeycomb 3.2.1,我无法让浏览器停止接受cookie.我有以下代码:

first.html:

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="cookie.js"></script>
        <script type="text/javascript">
            setCookie('testing','test cookie',365);
            window.location.href = 'second.html';
        </script>
    </head>
    <body>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

second.html:

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="cookie.js"></script>
        <script type="text/javascript">
            var temp = getCookie('testing');
            alert(temp);
        </script>

    </head>
    <body>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

cookie.js:

function setCookie(c_name,value,exdays)
{
    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;
}

function getCookie(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);
        }
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我关闭cookie并在我的任何桌面浏览器上访问first.html,我会被重定向并获得一个按预期显示为null的警报.

如果我打开我的cookie并在我的任何桌面浏览器上访问first.html,我会被重定向并获得一个警告,按预期显示"测试cookie".

现在,如果我在禁用cookie的Android平板电脑上运行此功能,它始终会在警报中返回"测试cookie".如果我打开或关闭cookie都没关系.我尝试更改设置,删除cookie和缓存,重新启动浏览器,甚至重新启动平板电脑,所有结果都相同.

任何帮助表示赞赏.

Dan*_*ist 1

在返回 cookie 之前检查 cookie 是否启用如何:

function getCookie(c_name)
{
    if(navigator.cookieEnabled) {
      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);
          }
      }
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)