views:

1727

answers:

15

I assume that you can't use a javascript code snippet to validate if the browser user has turned off javascript. So what can i use instead? Can someone offer a code sample?

I'm looking to wrap an if/then statement around it.

I often code in CFML, if that helps.

+5  A: 

Use the <noscript> HTML tags.

Daniel Jennings
+1  A: 
<noscript>
    ...some non-js code
</noscript>
Chris Marasti-Georg
A: 

You might have javascript execute some AJAX query and check to see if it has. Those that download the page and don't execute the query either have JS disabled or they're robots.

Kyle Cronin
+2  A: 

He's asking for a check to see if javascript is enabled. I can only think of doing exactly what the OP said - try using some Javascript with an interval to send a callback if JS is activated - unfortunately I don't think you can check server side whether JS is enabled which is why you use tags rather than render different content from the server.

Graphain
A: 

Really all you can do is put some message in the tags. I seem to remember trying this on ASP.NET somewhere, but you can really only tell if the browser supports Javascript, not whether or not it is actually allowed/enabled.

Daniel Huckstep
A: 

I don't know much about CFML, but .NET has the ability to detect browser capabilities. It does not, however, have the ability to detect if the browser is capable of javascript, but has it turned off. So, you're stuck there too.

Besides the HTML noscript tag, there's not much you can do, as far as I know, besides writing javascript progressively (see progressive enhancement) so that you don't need to check for Javascript:off.

EndangeredMassa
+4  A: 

Not sure what you are trying to do but if you just need to inform the user that Javascript is required you can just use the '<noscript>' tag. If you need to know on the server you could make an Ajax style request to the server from javascript. If you get the request javascript is working otherwise its not.

Timbo
A: 

I don't know JS, but would it be possible to modify the links inside the page with JS? If someone goes to the unmodified link, they're not using JS, but if they do then they are using JS. Does this make any sense?

Kyle Cronin
+1  A: 

Are we talking about something like this:

JavaScript:

<body>
...
...
<script type="text/javascript">
<!--
document.write("Hello World!")
//-->
</script>
<noscript>Your browser does not support JavaScript!</noscript>
...
...
</body>
A: 

Have never worked out how to do it without a round trip, so it depends on what your goal is.

If they have to have javascript to proceed, then I have (in .net) done things like disabling the login button at the server side, then enabled it client side it with javascript, and used a noscript tag to show an error message.

If it has to work either way, you can use progressive enhancement, and / or use js to set a hidden field and then set a session variable, but this means that you don't know until they get to the second request.

seanb
+4  A: 

this is a total hack but you could use an iframe inside the noscript tag to trigger an HTTP GET on a url to tell the server that a user doesn't have javascript enabled.

<body>
...
...
<noscript>
    <iframe src ="/nojs.aspx?SOMEIDENTIFIER=XXXX&NOJS=TRUE" style="display: none;">
    </iframe>
</noscript>
...
...
</body>
Jared
A: 

Note: This has been asked here on SO before. I actually have a blog post laying out how to just that http://unkwndesign.com/blog/?p=10 This specifically is used for logging, when the Google Analytics cant, because of the lack of javascript but It should work.

Unkwntech
This link is broken. Do you have a replacement?
Bill the Lizard
+1  A: 

Yes that NoScript snippet is right.

Registered User
+2  A: 

If you use Unobtrusive JavaScript then you don't need to check whether the user has JavaScript enabled.

If they have got JavaScript enabled then they'll get the full effect, but if they haven't then users will still be able to use your site. And as well as being better for accessibility you might find this approach boosts your SEO.

Ian Oxley
A: 

you could write

<script type="text/javascript" language="javascript">document.write("<input type='hidden' name='hasJs' value='1' />");

or otherwise write a cookie via js and then read it at the server if you want to check server side for js.

abigblackman