Is it possible to check if cookies are enabled with modernizr?

后端 未结 4 1898
北荒
北荒 2021-01-12 07:48

I was researching about how to check if the cookies are enabled in a browser and i found a lot of answer, i even tested a few ones, but after that a friend of mine

4条回答
  •  有刺的猬
    2021-01-12 08:21

    A direct answer to the question is 'Yes!' and it is built in

    Example code:

    if (Modernizr.cookies == false) {
    
        alert('Please enable cookies');    
    }
    else { 
        // do something with cookies
    }
    

    You can also use the css class .cookies or .no-cookies to show/hide a panel telling the user they need cookies enabled.

    .cookies #noCookies
    {
        display: none;
    }
    
    
    This site requires cookies! Please turn them on already!

    (This .cookies class is added to tag by Modernizr).

    Note: If you are creating a custom build of Modernizr the cookies option is currently 'hidden' under the 'Non-core detects' section.

提交回复
热议问题