How to detect if JavaScript is disabled?

后端 未结 30 3258
暗喜
暗喜 2020-11-21 07:37

There was a post this morning asking about how many people disable JavaScript. Then I began to wonder what techniques might be used to determine if the user has it disabled.

相关标签:
30条回答
  • 2020-11-21 08:10

    <noscript> isn't even necessary, and not to mention not supported in XHTML.

    Working Example:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
    <html>
    <head>
        <title>My website</title>
        <style>
          #site {
              display: none;
          }
        </style>
        <script src="http://code.jquery.com/jquery-latest.min.js "></script>
        <script>
          $(document).ready(function() {
              $("#noJS").hide();
              $("#site").show();
          });
        </script>
    </head>
    <body>
        <div id="noJS">Please enable JavaScript...</div>
        <div id="site">JavaScript dependent content here...</div>
    </body>
    </html>
    

    In this example, if JavaScript is enabled, then you see the site. If not, then you see the "Please enable JavaScript" message. The best way to test if JavaScript is enabled, is to simply try and use JavaScript! If it works, it's enabled, if not, then it's not...

    0 讨论(0)
  • 2020-11-21 08:10

    You can use a simple JS snippet to set the value of a hidden field. When posted back you know if JS was enabled or not.

    Or you can try to open a popup window that you close rapidly (but that might be visible).

    Also you have the NOSCRIPT tag that you can use to show text for browsers with JS disabled.

    0 讨论(0)
  • 2020-11-21 08:10

    A common solution is to the meta tag in conjunction with noscript to refresh the page and notify the server when JavaScript is disabled, like this:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <noscript>
                <meta http-equiv="refresh" content="0; /?javascript=false">
            </noscript>
            <meta charset="UTF-8"/>
            <title></title>
        </head>
    </html>
    

    In the above example when JavaScript is disabled the browser will redirect to the home page of the web site in 0 seconds. In addition it will also send the parameter javascript=false to the server.

    A server side script such as node.js or PHP can then parse the parameter and come to know that JavaScript is disabled. It can then send a special non-JavaScript version of the web site to the client.

    0 讨论(0)
  • 2020-11-21 08:10

    You don't detect whether the user has javascript disabled (server side or client). Instead, you assume that javascript is disabled and build your webpage with javascript disabled. This obviates the need for noscript, which you should avoid using anyway because it doesn't work quite right and is unnecessary.

    For example, just build your site to say <div id="nojs">This website doesn't work without JS</div>

    Then, your script will simply do document.getElementById('nojs').style.display = 'none'; and go about its normal JS business.

    0 讨论(0)
  • 2020-11-21 08:15

    In some cases, doing it backwards could be sufficient. Add a class using javascript:

    // Jquery
    $('body').addClass('js-enabled');
    
    /* CSS */
    .menu-mobile {display:none;}
    body.js-enabled .menu-mobile {display:block;}
    

    This could create maintenance issues on anything complex, but it's a simple fix for some things. Rather than trying to detect when it's not loaded, just style according to when it is loaded.

    0 讨论(0)
  • 2020-11-21 08:18

    Add this to the HEAD tag of each page.

    <noscript>
            <meta http-equiv="refresh" runat="server" id="mtaJSCheck" content="0;logon.aspx" />
    </noscript>
    

    So you have:

    <head>
        <noscript>
            <meta http-equiv="refresh" runat="server" id="mtaJSCheck" content="0;logon.aspx" />
        </noscript>
    </head>
    

    With thanks to Jay.

    0 讨论(0)
提交回复
热议问题