Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect cookie support? #191

Closed
tauren opened this issue Feb 13, 2011 · 4 comments
Closed

Detect cookie support? #191

tauren opened this issue Feb 13, 2011 · 4 comments

Comments

@tauren
Copy link

tauren commented Feb 13, 2011

Perhaps I missed it, but wouldn't it make sense for Modernizr to detect if cookies are supported? I haven't tested this, but something like this might work:

Modernizr.addTest('cookies', function () {
  // 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;
});

I'm not sure if adding the test for navigator.cookieEnabled will speed things up on browsers that have that host property or not. But its in there just in case.

Perhaps this could be taken further to detect session cookie support vs. persistent cookie support.

@paulirish
Copy link
Member

done in 33f00fb

thx

@onassar
Copy link

onassar commented Mar 9, 2020

Anyone running into issues with this related to SameSite flags? Specifically: this fails when attempting to set a cookie within an iframe, but that's because the SameSite isn't set to none (which is what I want to test for).

@onassar
Copy link

onassar commented Mar 17, 2020

Any thoughts on this @paulirish or @tauren ?

@uniejo
Copy link

uniejo commented Jun 20, 2022

I had the same problem with SameSite warnings. The following change, eliminates that warning, by adding SameSite=none and Secure options to the cookie.

  Modernizr.addTest('cookies', function() {
    // navigator.cookieEnabled cannot detect custom or nuanced cookie blocking
    // configurations. For example, when blocking cookies via the Advanced
    // Privacy Settings in IE9, it always returns true. And there have been
    // issues in the past with site-specific exceptions.
    // Don't rely on it.

    // try..catch because some in situations `document.cookie` is exposed but throws a
    // SecurityError if you try to access it; e.g. documents created from data URIs
    // or in sandboxed iframes (depending on flags/context)
    try {
      // Create cookie
      document.cookie = 'cookietest=1; samesite=none; secure';
      var ret = document.cookie.indexOf('cookietest=') != -1;
      // Delete cookie
      document.cookie = 'cookietest=1; samesite=none; secure; expires=Thu, 01-Jan-1970 00:00:01 GMT';
      return ret;
    }
    catch (e) {
      return false;
    }
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants