How to detect if a user had javascript disabled?

这一生的挚爱 提交于 2019-12-06 08:17:28

问题


I was talking with a friend about users who do not have javascript enabled in their browser and what could be done to show them a "no-javascript" version of your website.

Is it possible and how can it be done?

Thoughts?


回答1:


Don't try to build separate JS and non-JS versions of the site. Build a non-JS version and then enhance it with JS. This makes it easier to reuse code, allows you to use object/feature detection for the entire stack, and makes it less likely that users without JavaScript will be left behind if developers update one branch of the site but not the other.




回答2:


If you mean javascript, look up the <noscript> HTML tag.




回答3:


If you are talking about JavaScript and want an alternate version of your site for those without, simply give them site without and put this at the top of your page:

<script type="text/javascript">
     location.replace('http:javascript-version-of-your-site');
</script>



回答4:


Like this:

<html>
<head>
<noscript>
<meta http-equiv="refresh" content="0;url=http://foo/noscript.htm" />
</noscript>
</head>
<body>
...

The meta refresh (declarative redirect) only gets executed if there is no script enabled on the browser. Of course, the noscript tag is only understood by browsers that have a javascript engine. If you're trying to catch browsers that have NO javascript at all (I don't know of many) then this won't work.




回答5:


I use this combination of JavaScript and CSS

<html>
<style type="text/css">
    .js-on
    {
        display: none;
    }
    .js-off
    {
        background: red;
        color: White;
    }
 </style>
 <div id='jsdetect' class='js-off'><b>Javascript is OFF</b></div>
 <script type="text/javascript">
    document.getElementById('jsdetect').className = 'js-on';
 </script>

</html>

It's useful if your only aim is to tell the users that Javascript is not available and that the site will not work.




回答6:


You can put some code to log/track/alert you about users who have JavaScript disabled by using the tag.

<noscript>
   // Do Something
</noscript>



回答7:


Another option open to you rather than the obvious noscript tag is to use JavaScript to drop a cookie, and then test for that cookie and deliver content appropriately. javascript ver vs non redirect or span

just thought I'd throw that in there..



来源:https://stackoverflow.com/questions/2940662/how-to-detect-if-a-user-had-javascript-disabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!