Question

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.

Was it helpful?

Solution 4

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>

OTHER TIPS

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>

Use the <noscript> HTML tags.

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.

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.

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.

<noscript>
    ...some non-js code
</noscript>

Yes that NoScript snippet is right.

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.

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.

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.

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?

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.

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.

if you are looking for a way to check it server side, you're can send the user a js that puts a cookie.... if the cookie exist on a a request then you can tell if the scripted worked or not!

One reliable way to do this is using javascript's $.post to send a note to your server. (Apologies if there's any errors in this, written off top of my head, will update later when I get around to testing). Should allow you to build it so you can even pull from session data if they're using javascript, which will allow you to serve up replacements for javascript without having to resort to .

Your on-page script:

<script>
function myJavascriptTest(){
    $.post ()('myJavascriptTest.php', {myJavascriptOn: true}, function(){
    return true;
}
myJavascriptTest()
</script>

And in the targeted .php...

<?php

if ($_POST['myJavascriptOn'] == true){
    $_SESSION['javascriptIsOn'] = true;
} else {
    $_SESSION['javascriptIsOn'] = false;
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top