How to detect that JavaScript and/or Cookies are disabled?

前端 未结 9 1201
鱼传尺愫
鱼传尺愫 2020-11-27 14:13

How to detect that JavaScript or Cookies are disabled in the user\'s browser and notify him any help ?

9条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 15:09

    As the cookie detection didn't work in IE 11, I suggest the Modernizr approach:

    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;
        }
    }
    

    https://github.com/Modernizr/Modernizr/blob/master/feature-detects/cookies.js

提交回复
热议问题