简体   繁体   English

在ASP.NET MVC中启用了对Javascript的测试

[英]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. 我一直在寻找一种方法来测试用户是否启用了Javascript或在ASP.NET MVC中支持它的浏览器,如果他们没有,那么将它们路由到一个页面,告诉他们网站需要它工作。

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. 如果禁用javascript而不必调用函数,隐藏/显示div并担心DOM更新的闪存,则会重定向到另一个页面。

I know this has already been answered, but I haven't seen this approach anywhere on SO. 我知道这已经得到了解答,但我还没有在SO的任何地方看到这种方法。 If there is a reason, someone let me know :) 如果有原因,有人让我知道:)

You can use the <noscript> element. 您可以使用<noscript>元素。 http://www.w3schools.com/TAGS/tag_noscript.asp . 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. 您可以使用它来检查是否启用了JS并将结果缓存在会话中。

the <noscript> option seems to be the most applicable. <noscript>选项似乎是最适用的。

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的情况下使您的网站完全可访问,然后使用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; 您可以检查浏览器功能,它是HttpRequestBase的一部分,它是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. 但是,这不是确定的,并且确保您的应用程序在JavaScript不工作的最佳方法是使用不显眼的JavaScript方法设置您的应用程序。

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. 这可以确保您的应用程序像任何服务器端应用程序一样工作,然后使用JavaScript来接管链接,按钮点击等,以启用客户端AJAX功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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