jQuery detecting cookies enabled

前端 未结 6 971
失恋的感觉
失恋的感觉 2020-12-03 07:10

I have a jQuery-based web app. My requirement is fairly simple: I want to use jQuery to find out if the user has enabled or disabled cookies in their web browser. I\'m aware

相关标签:
6条回答
  • 2020-12-03 07:38

    I like this 1 liner function:

    function cookiesEnabled() {
        return $.cookie('check', 'valid', { expires: 1 }) && $.cookie('check') == 'valid';
    }
    
    0 讨论(0)
  • 2020-12-03 07:47

    You don't need jQuery for that, you can use vanilla Javascript:

    function are_cookies_enabled()
    {
        var cookieEnabled = (navigator.cookieEnabled) ? true : false;
    
        if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled)
        { 
            document.cookie="testcookie";
            cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
        }
        return (cookieEnabled);
    }
    

    http://sveinbjorn.org/cookiecheck

    0 讨论(0)
  • 2020-12-03 07:47

    Why are you testing with the property cookieEnabled? just try to create a cookie and check if it's done. And in case cookies work, delete the test created cookie.

     function are_cookies_enabled()
        {
                document.cookie="testcookie=valid";
                cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
    
                if(cookieEnabled){
                   document.cookie = "testcookie=; expires=Thu, 01 Jan 1970 00:00:00 UTC;"; 
                }
    
                return cookieEnabled;
        }
    
    0 讨论(0)
  • 2020-12-03 07:47

    You can simply set a test_cookie when loading your page. then delete if it is set, else alert('please enable cookies...');

        <script>
      jQuery(document).ready(function()
      {
        var TEST_COOKIE = 'test_cookie';
        jQuery.cookie( TEST_COOKIE, true );
        if ( jQuery.cookie ( TEST_COOKIE ) )
        {
          jQuery.cookie( TEST_COOKIE, null );  // delete the cookie
          alert( 'Good news, cookies are enabled.' );
        }
        else
        {
          alert( 'Cookies not enabled. Please enable and try again.' );
        }
      }
    </script>
    
    0 讨论(0)
  • 2020-12-03 07:49

    The simplest way is to check the navigator's property which contains the browser's information. Reference

    You could use navigator.cookieEnabled === true to detect the browser's support for cookie.

    0 讨论(0)
  • 2020-12-03 07:51

    Internet Explorer (version 10, at least) always returns true for navigator.cookieEnabled. Here's a method to work around this:

    function areCookiesEnabled() {
        var cookieEnabled = navigator.cookieEnabled;
    
        // When cookieEnabled flag is present and false then cookies are disabled.
        if (cookieEnabled === false) {
            return false;
        }
    
        // try to set a test cookie if we can't see any cookies and we're using 
        // either a browser that doesn't support navigator.cookieEnabled
        // or IE (which always returns true for navigator.cookieEnabled)
        if (!document.cookie && (cookieEnabled === null || /*@cc_on!@*/false))
        {
            document.cookie = "testcookie=1";
    
            if (!document.cookie) {
                return false;
            } else {
                document.cookie = "testcookie=; expires=" + new Date(0).toUTCString();
            }
        }
    
        return true;
    }
    
    0 讨论(0)
提交回复
热议问题