cookies.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /*!
  2. {
  3. "name": "Cookies",
  4. "property": "cookies",
  5. "tags": ["storage"],
  6. "authors": ["tauren"]
  7. }
  8. !*/
  9. /* DOC
  10. Detects whether cookie support is enabled.
  11. */
  12. define(['Modernizr'], function(Modernizr) {
  13. // https://github.com/Modernizr/Modernizr/issues/191
  14. Modernizr.addTest('cookies', function() {
  15. // navigator.cookieEnabled cannot detect custom or nuanced cookie blocking
  16. // configurations. For example, when blocking cookies via the Advanced
  17. // Privacy Settings in IE9, it always returns true. And there have been
  18. // issues in the past with site-specific exceptions.
  19. // Don't rely on it.
  20. // try..catch because some in situations `document.cookie` is exposed but throws a
  21. // SecurityError if you try to access it; e.g. documents created from data URIs
  22. // or in sandboxed iframes (depending on flags/context)
  23. try {
  24. // Create cookie
  25. document.cookie = 'cookietest=1';
  26. var ret = document.cookie.indexOf('cookietest=') !== -1;
  27. // Delete cookie
  28. document.cookie = 'cookietest=1; expires=Thu, 01-Jan-1970 00:00:01 GMT';
  29. return ret;
  30. }
  31. catch (e) {
  32. return false;
  33. }
  34. });
  35. });