tags:

views:

63

answers:

3

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

+4  A: 

Use <noscript> tag:

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

More Info:

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

Sarfraz
I don't think that is the question he asked.
George Marian
@George Marian: That's what it sounds, what do you think then?
Sarfraz
@sAc He's asking about detecting if it is disabled, not how to display something for users that have it disabled. The <noscript> tag doesn't tell you that a user has JavaScript disabled. It merely provides some alternate content to display when it is disabled. By convention, this is usually a message stating the JavaScript is required. See Oded's answer and the link provide by Ciarán Archer in his comment to the question.
George Marian
@George Marian but the `<noscript>` content *can* tell the server that Javascript is disabled. It can contain `<form>` or `<a>` links with parameters that convey to the server that Javascript is not enabled. Alternatively, of course, Javascript could post back an XMLHttpRequest to tell the server that Javascript *is* enabled.
Pointy
@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
@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
@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 <noscript> and use that to detect that JavaScript is disabled. But, that isn't necessary. You can just as well use <noscript> to display a message to user and integrate that JavaScript detection somewhere else on the page.
George Marian
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
@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
+4  A: 

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
<noscript> man, really.. I mean, REALLY
elcuco
@elcuco - And as a programmer, how do you _find out_ which one was used? He is not asking about outputting `<noscript>`, but about finding out if javascript is supported. This way, that data is communicated to the server.
Oded
The `<noscript>` block can contain links with embedded flag arguments, which would operate much like the cookie.
Pointy
@Pointy - true, however, you will not be able to use any link _outside_ the `<noscript>` block to make the distinction.
Oded
+2  A: 

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 : http://stackoverflow.com/questions/121203/how-to-detect-if-javascript-is-disabled

Ciaran Archer