简体   繁体   中英

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?

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 :)

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.

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.

禁用JavaScript时,您可以使用html标记在浏览器中显示消息。

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM