0

I need to check if cookies are enabled on my website and I'm running into problems with IE11 when the settings for privacy are set to high or to block all cookies. I've been using Javascript and followed the answer from this question

The code is as follows:

    function checkCookie() {
        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) ? true : showCookieFail();
    }

I also tried without the first line as suggested in the comments since navigator.cookieEnabled = true when the settings are set to high or to block all cookies

    function checkCookie2() {
        document.cookie = "testcookie";
        var cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
        return (cookieEnabled) ? true : showCookieFail();
    }

Both these options return true so showCookieFail() is never executed. Is it something I'm missing in the code or is there some other workaround that needs to be made for IE?

Community
  • 1
  • 1
Benji
  • 615
  • 5
  • 11
  • 25

1 Answers1

0

Are you sure that cookies are disabled within the browser? As the first block of code you pasted above seemed to work for me on IE 11 when I turned the cookies settings to block all. It called the showCookieFail() function.

pretzo246
  • 125
  • 9
  • After some closer investigation I do indeed see that the cookie is created even though I did try all different sorts of cookies setting to try and block them in IE. So even though I put the "block all cookies" setting they are still being created – Benji Oct 17 '16 at 15:08
  • This was indeed the problem and for some reason IE kept the cookies as sessions cookies when I was using VS and testing IE which I can't explain. But I tried pushing the changing to a live setting and it worked perfectly there – Benji Oct 17 '16 at 15:30
  • Perfect, glad you figured it out! – pretzo246 Oct 17 '16 at 15:32