dswatik
dswatik

Reputation: 9209

Testing for Javascript is enabled in ASP.NET MVC

I've been looking for a way to test to see if the user has Javascript enabled or a browser that supports it in ASP.NET MVC, and if they don't then route them to a page that tells them the site requires it to work.

However I haven't been able to find a definative answer to this..any suggestions?

Upvotes: 4

Views: 6533

Answers (5)

Chirag
Chirag

Reputation: 4186

You can use html tag to display a message in browser, when JavaScript is disabled.

<noscript>Your browser has disabled JavaScript.</noscript>

Upvotes: 1

Tommy
Tommy

Reputation: 39807

I have been searching for the best ways to do this tonight as well and came across, I think, an ingenious solution.

<noscript>
  <meta http-equiv="refresh" content="0;URL=http://yoursite.com/noJavascript" />
</noscript>

This redirects to another page if javascript is disabled without having to call functions, hide/show divs and worry about the flash of the DOM updating.

I know this has already been answered, but I haven't seen this approach anywhere on SO. If there is a reason, someone let me know :)

Upvotes: 7

Jon Erickson
Jon Erickson

Reputation: 114846

function isJavaScriptEnabled() {
    return true;
}

I'm actually semi-serious about this. You can use this to check if JS is enabled and cache the result in a session.

the <noscript> option seems to be the most applicable.

what you should strive to do is to make your site completely accessible without javascript enabled and then progressively enhance the site with javascript.

Upvotes: 1

Myles
Myles

Reputation: 21500

You can use the <noscript> element. http://www.w3schools.com/TAGS/tag_noscript.asp. This wouldn't redirect them to another page, but you could put a link they can click on to go to another page.

Upvotes: 3

Brian Mains
Brian Mains

Reputation: 50728

You can check the browser capabilities, which is part of HttpRequestBase, which is part of the httpcontextbase; however, this isn't definitive and the best way to ensure your app works when JavaScript doesn't is to setup your app using an unobtrusive javascript approach.

This ensures that your app works like it would any server-side app, then you use JavaScript to take over links, button clicks, etc. to enable the client-side AJAX features.

Upvotes: 0

Related Questions