1

Possible Duplicate:
How to detect if JavaScript is disabled?

How to find whether javascript is supported by user's browser or not

Community
  • 1
  • 1
Niraj Choubey
  • 3,942
  • 18
  • 58
  • 93

3 Answers3

6

You can use javascript to put some value into a hidden field in your form - if the field has this value on postback, then the browser supports javascript.

document.getElementById("myHiddenField").value = "js is on";

<input type="hidden" />

Another option is to use javascript to add a cookie and look for this cookie on the server side - if it is there, javascript is enabled, if not, it isn't.

I would have suggested <noscript>, but this doesn't help with your code finding out if javascript is supported (perhaps with an additional form only outputted in there), but this would complicate your client-side and server-side logic.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • man, really.. I mean, REALLY – elcuco Jul 03 '10 at 09:48
  • 1
    @elcuco - And as a programmer, how do you _find out_ which one was used? He is not asking about outputting ` – Oded Jul 03 '10 at 09:49
  • The ` – Pointy Jul 03 '10 at 12:37
  • @Pointy - true, however, you will not be able to use any link _outside_ the ` – Oded Jul 03 '10 at 16:19
5

You cannot check using Javascript if the browser is configured to use Javascript because obviously it can't run:)

So instead you use a <noscript> tag. This tag will always appear when Javascript is not supported and therefore you can put information within this tag to warn the user to enable Javascript on their browser (if they can).

Oh and this is a possible duplicate of : How to detect if JavaScript is disabled?

Community
  • 1
  • 1
Ciaran Archer
  • 12,316
  • 9
  • 38
  • 55
4

Use <noscript> tag:

<noscript>
  Your browser does not support javascript.......
</noscript>

More Info:

http://www.w3schools.com/tags/tag_noscript.asp

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • I don't think that is the question he asked. – George Marian Jul 03 '10 at 10:09
  • @George Marian: That's what it sounds, what do you think then? – Sarfraz Jul 03 '10 at 10:31
  • @sAc He's asking about detecting if it is disabled, not how to display something for users that have it disabled. The – George Marian Jul 03 '10 at 10:47
  • @Pointy Only if the user interacts with those elements. If the user never submits the form (or doesn't click on the link) you don't know. You do bring up a very good point, the absence of an expectation is a way to make this determination. I.e. if JavaScript does/doesn't post back, along the lines of Oded's answer, but using a different approach. – George Marian Jul 03 '10 at 12:49
  • @George Yes, that's true, but without Javascript the server can't know what's going on at the client end until *something* happens. – Pointy Jul 03 '10 at 14:05
  • @Pointy Yes I understand that. The example provided merely displays a message to the user. As much as I don't believe that is answering the question, I wouldn't downvote these answers since the question isn't clear. You are right, you can place some appropriate HTML in – George Marian Jul 03 '10 at 23:18
  • I'm not downvoting anything @George! I just add comments to clarify and to put in my own opinion. There's absolutely nothing wrong about your comments, so I apologize if I gave the impression that I think there is. – Pointy Jul 04 '10 at 00:00
  • @Pointy No worries, I didn't take it as such. I merely interpreted your statements as being pedantic (too picky) in this situation. Also, I didn't think you were downvoting anything. I ment that while I thought this answer was wrong, in that it wasn't answering the actual question being asked, I wouldn't downvote it since there's some confusion over the question itself. – George Marian Jul 04 '10 at 00:05