0

I am trying to detect Javascript Enabled or Disabled but from the following code, I am not getting the exact output.

<script language="Javascript" type="text/javascript">
function detectJS() {
    document.getElementById("displayJS").innerHTML=true?"Enabled":"Disabled";
}
</script>

<body>
<div id="detectJS"></div>
</body>

Where am I going wrong? Please help. Thanks in advance.

3
  • 2
    Why would you detect js? If your javascript executes, then it is enabled. If not, disabled. var jsEnabled = true seems kind of redundant. Commented Apr 2, 2013 at 17:03
  • Giving an element a specific id doesn't mean it will execute a function with the same name. Commented Apr 2, 2013 at 17:05
  • 2
    I learned this method to tell whether I am alive or dead: you just yell "I'm aliiiive!!", and if you can hear yourself, you're alive. If you don't hear anything, then (sorry to say) you're quite dead, and you should go to the nearest hospital immediately to see if they can revive you. Commented Apr 2, 2013 at 17:36

3 Answers 3

1

Use <noscript>. The browser will show the contents only when JavaScript is disabled.

Another alternative is to hide an element on page-load.

<div id="nojs">You must enable JavaScript!</div>
<script type="text/javascript">
    document.getElementById("nojs").style.display = "none";
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

All I can suggest is to use the "noscript" HTML tag, cause obviously you can't use a javascript function when javascript is disabled...

Comments

-1

Obviously using JS to detect JS is the wrong approach but here is why your code isn't working:

You need to call your method and the ID you're looking for is wrong: http://jsfiddle.net/FpY3A/

<script language="Javascript" type="text/javascript">
function detectJS() {
    document.getElementById("displayJS").innerHTML=true?"Enabled":"Disabled";
}
detectJS();
</script>

<body>
<div id="displayJS"></div>
</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.