如果使用noscript标记在客户端禁用了javascript,则显示消息

Tri*_*der 17 javascript noscript

如果在客户端禁用了javascript,我想标记一条消息.我在这里搜索并找到<noscript>用于处理这些东西的标签.

我在w3schools编辑做了这个检查,但它没有工作让我知道这<noscript>是不是意味着这个或我在这部分缺少的其他东西?

在此输入图像描述

Rub*_*ist 39

试试这个 :-

How to detect JavaScript is disabled in browser?
Run Code Online (Sandbox Code Playgroud)

我们知道,tag用于JavaScript.同样的方式是在浏览器中禁用JavaScripts时可以使用标记.

<script>Put Sample code here for execution when JavaScript is Active </script>
<noscript>Put Sample code here for execution when JavaScript is Disabled</noscript>
Run Code Online (Sandbox Code Playgroud)

如何在浏览器中处理禁用的JavaScript?

禁用JavaScript时,只是尝试重定向到某个页面,我们可以在其中显示禁用Javascript的消息.HTML中有一个名为"元刷新"的元标记,它将用户重定向到该标题中指定的间隔中的另一个页面.

<noscript>
  <META HTTP-EQUIV="Refresh" CONTENT="0;URL=ShowErrorPage.html">
</noscript>
Run Code Online (Sandbox Code Playgroud)

因为,我们可以在noscript里面看到上面的代码,有"meta refresh"标签,间隔为"0"秒.因为,在该页面中禁用了JavaScript,浏览器会被重定向到"ShowErrorPage.html"以显示一些警告消息.

我希望这能帮到您.


小智 9

你是对的.该<noscript>标记仅用于在禁用JavaScript时显示.要对此进行测试,请执行以下操作:

  • 将此代码段保存在"test.html"文件中.
  • 用你的浏览器打开它.
  • 启用/禁用JavaScript(在FireFox中,它位于:Tools/Options/Content/Enable JS).

正如您所看到的,您可以在标记中放置任何HTML,这些HTML<noscript>将放在页面正文中.

<html>
  <body>
    <h1>Simple Example Page</h1>
    <script type="text/javascript">
      document.write("Hi, JavaScript is enabled!");
    </script>
    <noscript>
      <div style="border: 1px solid purple; padding: 10px">
        <span style="color:red">JavaScript is not enabled!</span>
      </div>
    </noscript>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)


fra*_*ank 7

<noscript>
   <META HTTP-EQUIV="Refresh" CONTENT="0;URL=ShowErrorPage.html">
</noscript>
Run Code Online (Sandbox Code Playgroud)

因为IE11(和之前的版本)有一个安全选项,当设置为'high'时会禁用Javascript和Meta Refresh标签,这不是一个好的解决方案!

我发现处理这种情况的最佳解决方案是:

<noscript class="noscript">
   <div id="div100">
   Please enable javascript in your browser .... blah blah
   </div>
</noscript>

<style>
   body{
      position:relative;
   }
   .noscript {
      width:100%;
      height:100%; /* will cover the text displayed when javascript is enabled*/
      z-index:100000; /* higher than other z-index */
      position:absolute;
   }
   .noscript #div100{
       display:block;
       height:100%;
       background-color:white; 
   }
</style> 
Run Code Online (Sandbox Code Playgroud)