1

There are similar questions like this, this, and Check if third-party cookies are enabled, but none work with Safari 15.2.

Under Safari preferences, if you enable the "Block all cookies" option, `window.localStorage' generates an error.

This code fails to detect the situation.

function areCookiesEnabled() {
    try {
      document.cookie = 'cookietest=1';
      var cookiesEnabled = document.cookie.indexOf('cookietest=') !== -1;
      document.cookie = 'cookietest=1; expires=Thu, 01-Jan-1970 00:00:01 GMT';
      return cookiesEnabled;
    } catch (e) {
      return false;
    }
} 

Catching errors to window.localStorage is one approach, but is there a more robust, more recommended way to detect this situation in Safari 15.2? As indicated by other users, this approach fails in Chrome.

Crashalot
  • 33,605
  • 61
  • 269
  • 439

0 Answers0