如何检测Microsoft Edge / Windows 10上是否禁用了Cookie?

时间:2015-08-21 17:19:12

标签: javascript cookies microsoft-edge

我有一些代码适用于所有其他浏览器,但不适用于Windows 10 / Microsoft Edge。有任何想法吗?



function areCookiesEnabled() {
  // Quick test if browser has cookieEnabled host property
  if (navigator.cookieEnabled) {
    return true;
  }
  // Create cookie
  document.cookie = "cookietest=1";
  var ret = document.cookie.indexOf("cookietest=") !== -1;
  // Delete cookie
  document.cookie = "cookietest=1; expires=Thu, 01-Jan-1970 00:00:01 GMT";
  return ret;
}

if (areCookiesEnabled()) {
  alert("Cookies are enabled!");
} else {
  alert("Cookies are not enabled!");
}




Cookie在设置中被屏蔽

Cookies are blocked in settings

Stack Overflow表示已启用Cookie

Stack Overflow says that cookies are enabled

3 个答案:

答案 0 :(得分:0)

你可以创建一个cookie,然后测试它我有一个小而非常有用的库here,你可以做这样的事情

if(navigator.cookieEnabled||cookier.make("test","test",1))
//if one of these returns true {
    if(cookier.parse("test))
    //if test cookie is set
    {
        console.log("cookies are allowed");
    }
}

答案 1 :(得分:0)

我最终找到了一个Cookie库,通过实际创建一个真正的cookie来正确测试它:

https://github.com/ScottHamper/Cookies

使用的方法是Cookies.enabled。这是作者使用的代码部分:

 var testKey = 'cookies.js';
 var areEnabled = Cookies.set(testKey, 1).get(testKey) === '1';
 Cookies.expire(testKey);
 return areEnabled;

我在Win10 / Edge开发工具中尝试了这个,它对我有用。

答案 2 :(得分:0)

如果您不想再包含单独的js,可以按以下方式更新您的功能

function areCookiesEnabled() {
var cookieEnabled = navigator.cookieEnabled;

// When cookieEnabled flag is present and false then cookies are disabled.
if (cookieEnabled === false) {
    return false;
}

var isIE =  /*@cc_on!@*/false;
var isEdge = !isIE && !!window.StyleMedia;

// try to set a test cookie if we can't see any cookies and we're using 
// either a browser that doesn't support navigator.cookieEnabled
// or IE / Edge (which always returns true for navigator.cookieEnabled)
if ((cookieEnabled === null || isIE || isEdge)) {
    document.cookie = "testcookie=1";

    if (GetCookieValue("testcookie") != "1") {
        return false;
    } else {
        document.cookie = "testcookie=; expires=" + new Date(0).toUTCString();
    }
}

return true;
}

// Get cookie value by passing the key.
function GetCookieValue(strKey) {

// split cookies by '; ', keep space as it is.
var arrCookies = document.cookie.split('; ');

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

    var keyValuePair = GetKeyValuePair(arrCookies[i]);

    // Match the key.
    if (keyValuePair.key === strKey) {
        // return value of matched key.
        return keyValuePair.value;
    }
}

// Return an empty string if key is not present.
return "";
}

// Get key value pair from the cookie string.
function GetKeyValuePair(strCookie) {

// "=" is a valid character in a cookie value according to RFC6265, so cannot `split('=')`
var separatorIndex = strCookie.indexOf('=');

// IE omits the "=" when the cookie value is an empty string
separatorIndex = separatorIndex < 0 ? strCookie.length : separatorIndex;

var key = strCookie.substr(0, separatorIndex);
var decodedKey;

try {
    decodedKey = decodeURIComponent(key);
} catch (e) {
}

return {
    key: decodedKey,
    value: strCookie.substr(separatorIndex + 1)
};
};

我已经用这种方式解决了这个问题。