Hướng dẫn check cookie enabled javascript

Published January 27, 2021

To check whether a setting a cookie is enabled in the browser, you can use the cookieEnabled property in the window.navigator global object in JavaScript.

// check if cookie enabled in browser
const isCookieEnabled = navigator.cookieEnabled;

console.log[isCookieEnabled]; // true
  • The property will return a Boolean true if cookie enabled and return false if not enabled.

See this example live in JSBin.

Feel free to share if you found this useful 😃.

Share on: Facebook Twitter

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

asked Jan 5, 2011 at 10:59

Mahmoud SalehMahmoud Saleh

32.7k116 gold badges331 silver badges491 bronze badges

2

For checking cookies you can use:

function checkCookie[]{
    var cookieEnabled = navigator.cookieEnabled;
    if [!cookieEnabled]{ 
        document.cookie = "testcookie";
        cookieEnabled = document.cookie.indexOf["testcookie"]!=-1;
    }
    return cookieEnabled || showCookieFail[];
}

function showCookieFail[]{
  // do something here
}


// within a window load,dom ready or something like that place your:
checkCookie[];

And for checking JavaScript use a tag with some kind of message inside

answered Jan 5, 2011 at 11:01

robjmillsrobjmills

18.2k15 gold badges74 silver badges120 bronze badges

7

Update [6/25/18]:

A lot of these posts, including mine, are taking snippets from Modernizr. They will all eventually become outdated as the Modernizr code gets updated.

I think the best answer to this question should be to use Modernizr directly.

if [Modernizr.cookies] {
  // supported
} else {
  // not-supported
}

Original Answer [5/11/17]:

This is taken straight from Modernizr and works in more browsers than other solutions in this post.

//github.com/Modernizr/Modernizr/commit/33f00fbbeb12e92bf24711ea386e722cce6f60cc

function checkCookie[]{
    // Quick test if browser has cookieEnabled host property
    if [navigator.cookieEnabled] return true;
    // Create cookie
    document.cookie = "cookietest=1";
    var ret = document.cookie.indexOf["cookietest="] != -1;
    // Delete cookie
    document.cookie = "cookietest=1; expires=Thu, 01-Jan-1970 00:00:01 GMT";
    return ret;
}

answered May 11, 2017 at 18:57

Noah SolomonNoah Solomon

1,20314 silver badges23 bronze badges

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

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

mcmimik

1,08912 silver badges29 bronze badges

answered Jan 30, 2018 at 12:11

ZymotikZymotik

5,2643 gold badges36 silver badges46 bronze badges

3

Assuming JavaScript is enabled, this will tell you if cookies are enabled or not. Works in old browsers.

// returns 1 or 0 instead of true or false. Returns null if inconclusive.
function cookiesEnabled[] {
    var i, j, cookies, found;
    if [navigator.cookieEnabled===false] return 0;
    document.cookie = 'testcookiesenabled=1';
    for [i=0; i

Chủ Đề