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

问题描述 投票:11回答:4

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

如果没有启用JS,还有其他方法来显示消息吗?

谢谢,康拉德

javascript html5 browser
4个回答
36
投票

使用noscript标签:

<noscript>

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

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


4
投票

你可以使用noscript,如果用户禁用了javascript,这些标签内部会显示。

如果你想隐藏其他内容,如果用户没有启用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>

这只会在用户启用了javascript时显示内容。


1
投票

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

    <html>
    <body>

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

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

    </body>
    </html>

1
投票

我已经尝试了几个使用noscript的选项,但没有一个适合我。

我最终使用:

<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>

这将隐藏p标签,只有启用js。 这是检测JavaScript是否启用的opossite逻辑,但它适用于所有浏览器。

SRC

© www.soinside.com 2019 - 2024. All rights reserved.