Is there any way, via C# code, to detect whether JavaScript is enabled or disabled? If I'm using JQuery for developing a website, if JavaScript is not enabled, then what do I do? How should I handle it?
3 Answers
C# has no way of determining this. C# runs on the server and sends output to the client. It has no idea if the client is capable/willing to run JavaScript.
Also, a script can never truly know this either, since it's not executed anyway.
The only way you can "react" to disabled JavaScript would be to use a <noscript> tag to advise your users that JavaScript is required for your site.
You can see an example of this on StackOverflow. Turn off JavaScript, refresh the site, and you will see:
<noscript>
<div id="noscript-warning">Stack Overflow works best with JavaScript enabled<img src="http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif" alt="" class="dno"></div>
</noscript>
Comments
This article on CodeProject has an implementation that checks it for you, but as far as I could see it requires an initial roundtrip back to the browser to check whether javascript is enabled. I haven't tried it myself so I can't tell you whether it works or not, but I see no reason it shouldn't.
I hope this helps you in some way.
Comments
The best approach you can use is to employ a 'progressive enhancement' strategy to architecture your application. The best way to use javascript to enhance the usability is to start without it.
If you want a piece of code to detect an redirect to a no-javascript enabled page then u can use a simple approach with a simple page containing the following elements to detect and redirect the user to where u need:
1.- A META header with a redirect to a page which handles whenever the user not has javascript enabled. 2.- An script in the content which changes the 'location' to another page which handler whenever the user has javascript enabled.
A simple page will look like
<html>
<head>
<title>Sample Page</title>
<meta http-equiv="refresh" content="2;url=/page_no_javascript_enabled.aspx" />
</head>
<body>
<script language="javascript" type="text/javascript">
document.location='/page_with_javascript_enabled.aspx';
</script>
<h1>Please wait ...</h1>
</body>
</html>