【问题标题】:ie7 cookies not being blocked on my siteie7 cookie 在我的网站上没有被阻止
【发布时间】:2011-09-26 03:33:03
【问题描述】:

我正在为我的客户的网站添加一项功能,如果禁用 cookie,则会在登录页面上显示一个 div。我首先开始在 chrome 中测试这个,关闭 cookie,然后

if (document.cookies == "") {
    // show div to tell users cookies are disabled on their machine.
}

一切正常。同样在我的 Page_Load 代码隐藏中,我试图设置一个 cookie

protected void Page_Load(object sender, EventArgs e) 
{
    Response.Cookies["test"].Value = "test";
    Response.Cookies["test"].Expires = DateTime.Now.AddDays(1);
}

在 chrome 的土地上一切似乎都很好。接下来,我转到 IE7,阻止所有 cookie,并且为了安全起见,删除我所有的历史记录和 cookie,以防万一。我希望看到我的 div,但没有。

所以,我在 if (document.cookies == "" ) { } 中添加了一个 else 来读取 javascript 中的 cookie,果然有我的测试 cookie。

我进入工具 -> 互联网选项 -> 隐私选项卡 -> 并将滑块一直移动到顶部,“阻止所有 Cookie”。在隐私选项卡中,我单击了“高级”按钮并设置了第一方 cookie 和第三方 cookie 来提示。我觉得应该屏蔽掉。

例如,作为测试,我在 ie7 中访问 google.com,它会提醒我是否要允许或阻止来自 google 的两个 cookie。

我需要做些什么来检查 ie7 中禁用的 cookie 吗?

我创建了一个 cookies.js 文件,用于创建、读取和删除

function createCookie(name, value, days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
     }

     document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {

    //  we're going to search for the name of the cookie, followed by an =. So create this new string and put it in nameEQ
    var nameEQ = name + "=";

    //  split document.cookie on the semicolons. ca becomes an array containing all cookies that are set for this domain and path.
    var ca = document.cookie.split(';');

    for (var i = 0; i < ca.length; i++) {

        //  set c to the cookie to be checked.
        var c = ca[i];

        //  if the first character is a space, remove it by using the 'substring()' method. continue doing this until the first character is not a space.
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);

        //  now string c begins with the name of the current cookie. if this is the name of the desired cookie, we've found what we are looking for.
        //  we now only need to return the value of the cookie, which is the part of c that comes after nameEQ. By 
        //  returning this value we also end the function: mission accomplished.
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);

    }

    //  if after having gone through all cookies, we haven't found the name we are looking for, the cookie is not present, just return null.
    return null;

}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function cookiesEnabled() {
    if (document.cookies == "") {
       return false;
    }

    return true;
}

刚刚下载了 jquery.cookie.js,在我的检查 cookie 是否启用的函数中,我有这个:

function cookiesEnabled() {
    var TEST_COOKIE = 'test_cookie';
    $.cookie(TEST_COOKIE, true);
    if ($.cookie(TEST_COOKIE)) {
        $.cookie(TEST_COOKIE, null);
        return true;
    }
    return false;
}

这适用于 chrome 和 firefox,但不适用于 ie7。

我也试过这个:

function cookiesEnabled() {
    var cookieEnabled = (navigator.cookieEnabled) ? true : false;

    if (typeof navigator.cookieEnabled === "undefined" && !cookieEnabled) {
        document.cookie = "testcookie";
        cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
    }

    return cookieEnabled;
}

这确实有效。我认为在某一时刻或 navigator.cookieEnabled 由 ie 支持,但似乎 chrome 和 firefox 也支持它。

这件事真的开始让我紧张了!!!

【问题讨论】:

    标签: javascript asp.net cookies internet-explorer-7


    【解决方案1】:

    这是我找到的一个旧测试。

    它仍然适用于 IE 吗?

    对于其他浏览器,它需要一个 setCookie、getCookie 和 delCookie 函数,如果需要我也有这些函数

    function isCookieEnabled() {
       if (document.all) return navigator.cookieEnabled;
       setCookie('testcookie',today.getTime());
       var tc = getCookie('testcookie');
       delCookie('testcookie');
       return (tc == today.getTime());
    }
    
    /* 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 
          offset = document.cookie.indexOf(search) 
          if (offset != -1) { // if cookie exists 
             offset += search.length 
             // set index of beginning of value 
             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";
    //   alert(name+' marked for deletion');
    }
    

    【讨论】:

    • 如果你能提供 setCookie、getCookie 和 delCookie 那就太好了。
    • 使用我拥有的 cookies.js 文件,我尝试创建一个 cookie 并在 ie7 中的 javascript 中将其读回,所有 cookie 都被阻止,并且我能够读取我创建的 cookie。有什么想法吗?
    • 没有。听起来很奇怪。是在网络服务器上吗?你是否清除了缓存并关闭和打开 IE
    • 我清除了缓存,关闭并重新打开了 IE7。刚试了你的js代码,还是不行。我会将我的东西部署到我的网络服务器并在那里尝试。
    • 这是必须的。不要依赖从磁盘加载的文件在 IE 中发生的任何事情
    猜你喜欢
    • 2018-11-20
    • 2023-03-18
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    • 2021-05-11
    • 1970-01-01
    相关资源
    最近更新 更多