简体   繁体   English

使用class和document.documentElement检测js

[英]Detecting js using class and document.documentElement

Trying to figure out how this code is detecting if js is enabled in the web browser or not. 试图弄清楚此代码如何检测是否在Web浏览器中启用了js。 There is no any check or if or anything else. 没有任何检查,或者是否还有其他任何东西。 Message is displayed when there is class no-js that is simple. 当存在简单的no-js类时,将显示消息。 But where the actual checking is made? 但是在哪里进行实际检查?

    <!DOCTYPE html>
    <html lang="en" class="no-javascript">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            .warning {
                display: none;
            }

            .no-javascript .warning {
                display: block;
                text-align: center;
            }
        </style>
    </head>

    <body>
        <div class="warning">Enable js</div>
        <script>
            var elDocument = document.documentElement;
            elDocument.className = elDocument.className.replace(/(^|\s)no-javascript(\s|$)/, '$1');
        </script>
    </body>

    </html>

What this code does is display the warning initially, then immediately tries to hide it with Javascript. 该代码的作用是首先显示警告,然后立即尝试使用Javascript将其隐藏。 If Javascript is disabled, the code does not run and it cannot remove the warning, therefore the user sees it. 如果禁用了Javascript,则代码不会运行,并且无法删除警告,因此用户可以看到警告。

Be aware that there also is the tag that renders only if JavaScript is not enabled, cf. 请注意,还有一个仅在未启用JavaScript的情况下才会呈现的标记,请参见。 https://developer.mozilla.org/en-US/docs/Web/HTML/Element/noscript https://developer.mozilla.org/en-US/docs/Web/HTML/Element/noscript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM