3

I am trying to find that the cookies are enabled or disabled with javascript.

Detail description :

I have created a function in the javascript as below :

  if (navigator.cookieEnabled != true) {
               alert("Cookies are not enabled, Please enable cookies to continue logging in ProjectName");
            }

I have placed the function in the onload in the body of the page . Please note that this function works fine for the chrome and Firefox.

But it is not working for the IE11 , and the reason is the navigator.cookieEnabled always returns true even if the cookies are disable or blocked so please suggest other way or add comment to make this right. Also used document.cookies.length but it also returns true even if the cookies are disabled.

Trilok Pathak
  • 2,931
  • 4
  • 28
  • 34
  • Are you open to using a library - if so https://modernizr.com/ already has checks for this (check cookies in https://modernizr.com/docs) – potatopeelings Apr 14 '16 at 12:35
  • 1
    Possible duplicate of [jQuery detecting cookies enabled](http://stackoverflow.com/questions/8112634/jquery-detecting-cookies-enabled) – smnbbrv Apr 14 '16 at 12:37
  • @TrilokPathak according to the code you posted here you did not try any of the solutions they give there – smnbbrv Apr 14 '16 at 12:40
  • @smnbbrv yes have done all of it the comment of the Josh Schultz and Sarfraz too but it is not happening for me , Let me tell you where problem is. In the IE navigator.cookieEnabled always returns true. so in both the developer's comment it always returns me true although the cookies are disabled. – Trilok Pathak Apr 14 '16 at 13:07

1 Answers1

3

If you don't want to use a library you could just use the relevant bit from the library, like so (from https://github.com/Modernizr/Modernizr/blob/74655c45ad2cd05c002e4802cdd74cba70310f08/feature-detects/cookies.js)

function supportsCookies() {
    try {
      // 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;
    }
    catch (e) {
      return false;
    }
}
potatopeelings
  • 40,709
  • 7
  • 95
  • 119
  • here for the ie and chrome it is ret is true what tha means ? – Trilok Pathak Apr 14 '16 at 12:50
  • true means cookies are supported. So if you are getting true for IE and Chrome it means cookies are supported for your IE and Chrome configuration. – potatopeelings Apr 14 '16 at 12:59
  • In IE the cookies are disabled still code returns me true what i want is i want to find the browser setting the cookies are enable or not , by javascript. Please note that. – Trilok Pathak Apr 14 '16 at 13:02
  • hm.. that shouldn't happen because the script actually saves a cookie and retrieves it. May I ask why you think the cookies are disabled in IE? Is there some script that fails? – potatopeelings Apr 14 '16 at 13:08
  • Yes please , see above script i have written , it is not working for IE11 Only and i also know the reason , so if you have any other way or extra comment to solve the problem it will be big help !! – Trilok Pathak Apr 14 '16 at 13:08
  • As per the github page linked to in the answer `navigator.cookieEnabled cannot detect custom or nuanced cookie blocking...` so your above script would be incorrect, yes? – potatopeelings Apr 14 '16 at 13:12
  • Yes , the script works fine for Firefox and chome but it is not working for the IE and i am also not getting any other way to do the same. If you know any other way then please let me know or suggestion in current code. – Trilok Pathak Apr 14 '16 at 13:14
  • Any suggestion on the question ?? – Trilok Pathak Apr 27 '16 at 13:39
  • @TrilokPathak - unfortunately I've reached a dead end on this one. – potatopeelings Apr 27 '16 at 14:45