如何检测到JavaScript和/或Cookies被禁用?

如何检测用户浏览器中的JavaScript或Cookies已禁用并通知他有什么帮助吗?

Mahmoud Saleh asked 2020-02-22T02:45:54Z
8个解决方案
69 votes

要检查Cookie,您可以使用:

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();

为了检查JavaScript,请使用<noscript>标签,其中包含某种消息

robjmills answered 2020-02-22T02:46:09Z
19 votes

更新(6/25/18):

其中许多帖子(包括我的帖子)都采用了Modernizr的摘要。 随着Modernizr代码的更新,它们最终都将过时。

我认为这个问题的最佳答案应该是直接使用Modernizr。

if (Modernizr.cookies) {
  // supported
} else {
  // not-supported
}

原始答案(5/11/17):

这直接取自Modernizr,并且比本文中的其他解决方案可在更多浏览器中使用。

[HTTPS://GitHub.com/modern i自然/modern i自然/commit/33发00FBB EB12哦92部分24711EA386哦722猜测6发60曹操]

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;
}
Noah Solomon answered 2020-02-22T02:46:52Z
15 votes

由于cookie检测在IE 11中不起作用,我建议使用Modernizr方法:

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/modern i自然/modern i自然/blob/master/feature-detects/cookies.就是]

Zymotik answered 2020-02-22T02:47:17Z
5 votes
/**
* @hasCookies:  Check if cookie's are Enabled
* @param  {none} ''
* @return {BOOLEAN} true|false
*/
function hasCookies() {
return (navigator.cookieEnabled);
}
JimmyLandStudios.com answered 2020-02-22T02:47:33Z
1 votes

这是最简单的方法

Check if Cookies Enabled in Your Browser:  
<br />
Check if Cookies Enabled in Your Browser:  
<br />

Niresh answered 2020-02-22T02:48:58Z
1 votes

假设启用了JavaScript,它将告诉您是否启用了cookie。 在旧的浏览器中工作。

// returns 1 or 0 instead of true or false. Returns null if inconclusive.
function cookiesEnabled() {
    var i, j, cookies, found;
    document.cookie = 'testcookiesenabled=1';
    for (i=0; i<2; i++) {
        found = false;
        cookies = document.cookie.split(';');
        j = cookies.length;
        while(j--) {
            while (cookies[j].charAt(0)==' ') {// trim spaces
                cookies[j] = cookies[j].substring(1);
            }
            if (cookies[j].indexOf('testcookiesenabled=')==0) {
                found = true;
                break;
            }
        }
        if (!found) {
            return i;
        }
        // Delete test cookie.
        document.cookie = 'testcookiesenabled=; expires=Thu, 01 Jan 1970 00:00:01 GMT';
    }
    // Results inconclusive.
}

要仅在禁用JavaScript时显示消息,请使用

<noscript>JavaScript is disabled. Please enabled JavaScript.</noscript>
PHP Guru answered 2020-02-22T02:49:22Z
0 votes

此功能可以查看用户的错误消息,还可以停止脚本执行,并可以返回cookie状态。

function IS_Cookies_Enabled(Show_Message, Stop_Script)
{
    if(Show_Message == undefined){
        Show_Message = true;
    }

    if(Stop_Script == undefined){
        Stop_Script = false;
    }


    var Cookie_Status = navigator.cookieEnabled;
    if (!Cookie_Status)
    { 
        document.cookie = "Test";
        Cookie_Status = document.cookie.indexOf("Test") != -1;
        // Delete test cookies
        document.cookie = "Test=1; expires=Thu, 01-Jan-1970 00:00:01 GMT";

    }

    if((!Cookie_Status) && (Show_Message))
    {
        document.getElementsByTagName('body')[0].innerHTML = "<div style='width: 600px; max-width: 100%; margin: 100px auto;'><h1 style='color: red'>Cookies Required</h1><p style='font-size: 18px;'>Cookies are not enabled on your browser. <br>Please enable cookies in your browser preferences to continue.</p><strong>Sylingo</strong>";
    }

    if((!Cookie_Status) && (Stop_Script))
    {
        throw new Error('Cookies is disabled');
    }
    return Cookie_Status;
}

要使用它:

IS_Cookies_Enabled(true, true);  // Will stop script and view error message

IS_Cookies_Enabled(true, false);  // Just view error message

$Cookies_Status = IS_Cookies_Enabled(false, false);  // return cookies status

并用于检查JavaScript使用:

<noscript>
Javascript is not enabled on your browser. 
</noscript>
Mohamad Hamouday answered 2020-02-22T02:49:51Z
-1 votes

尝试<noscript>...</noscript>标签,它可以部分检查是否启用了JavaScript。

<script type="application/javascript">
    document.write("This text would be displayed if JavaScript is enabled");
</script>
<noscript>
    This text would be displayed if JavaScript is not enabled
</noscript>
kamal answered 2020-02-22T02:50:12Z
translate from https://stackoverflow.com:/questions/4603289/how-to-detect-that-javascript-and-or-cookies-are-disabled