Skip to content

Instantly share code, notes, and snippets.

@davalapar
Created December 4, 2018 20:47
Show Gist options
  • Save davalapar/397bc175bf099628cd456282790896a8 to your computer and use it in GitHub Desktop.
Save davalapar/397bc175bf099628cd456282790896a8 to your computer and use it in GitHub Desktop.
detect coookies
// 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 = 'yamomsahoe=1';
var ret = document.cookie.indexOf('yamomsahoe=') !== -1;
// Delete cookie
document.cookie = 'yamomsahoe=1; expires=Thu, 01-Jan-1970 00:00:01 GMT';
return ret;
}
catch (e) {
return false;
}
// https://github.com/Modernizr/Modernizr/blob/master/feature-detects/cookies.js
@davalapar
Copy link
Author

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