如果浏览器中未启用 javascript,则显示消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9391195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:31:18  来源:igfitidea点击:

Show message if javascript is not enabled in the browser

javascripthtmlbrowser

提问by Konrad

I'm looking for a solution to show the visitor of my website an info message, if he has no javascript enabled. I tried it with a message in a div, that is visible by default but immediately hidden by a jQuery function on start up. The problem is, that the message is visible for a short time (until it is hidden), what is very irritating.

我正在寻找一种解决方案来向我网站的访问者显示一条信息消息,如果他没有启用 javascript。我在 div 中尝试了一条消息,默认情况下该消息是可见的,但在启动时立即被 jQuery 函数隐藏。问题是,该消息在短时间内可见(直到它被隐藏),这是非常令人恼火的。

Are there other ways to show a message, if JS is not enabled?

如果未启用 JS,是否还有其他方式可以显示消息?

Thanks, Konrad

谢谢,康拉德

回答by Jakub Hampl

Use the noscripttag:

使用noscript标签:

<noscript>

  <div class="awesome-fancy-styling">
    This site requires JavaScript. I will only be visible if you have it disabled.
  </div>
  ...
</noscript>

See https://developer.mozilla.org/en/HTML/Element/noscript.

请参阅https://developer.mozilla.org/en/HTML/Element/noscript

回答by Kyle R

You can use noscript, inside these tags is what will display if the user has javascript disabled.

您可以使用noscript, 在这些标签中,如果用户禁用了 javascript,将会显示什么。

If you want to hide the other content if the user doesn't have javascript enabled, you can do something like so (this uses jquery):

如果您想在用户未启用 javascript 的情况下隐藏其他内容,您可以执行以下操作(使用 jquery):

<style type="text/css">
    .example {
        display: none;
    }
</style>

<script type="text/javascript">
    $(document).ready(function(){
        $('.example').show();
    });
</script>

<div class="example">
    <p>...</p>
</div>

<noscript>
    <p>You must have javascript enabled for the example div to show!</p>
</noscript>

This will only show the content if the user has javascript enabled.

如果用户启用了 javascript,这只会显示内容。

回答by Sameera Sampath

You can use '' and provide the error message you want.

您可以使用 '' 并提供所需的错误消息。

    <html>
    <body>

    <script language="javascript" type="text/javascript">
    <!--
        document.write("Hello World!")
     //-->
    </script>

    <noscript>
     Sorry...JavaScript is needed to go ahead.
    </noscript>

    </body>
    </html>

回答by CONvid19

I've tried several options using noscriptand noneworked for me.

我已经尝试了几个选项noscript但没有一个对我有用。

I ended up using :

我最终使用了:

<p align=center id=js_disabled_message>
    x.com requires JavaScript Enabled <br> <a href="https://www.enable-javascript.com/">www.enable-javascript.com</a>
</p>
<script>
    document.getElementById('js_disabled_message').style.display = 'none';
</script>

Which will hide the ptag, onlyif jsis enabled.
It's kinda of the opossite logic to detect if JavaScriptis enabled, but it works well in all browsers.

只有在启用时才会隐藏p标签。 这是一种检测是否启用的反向逻辑,但它适用于所有浏览器。js
JavaScript

SRC

资源中心