简体   繁体   English

检测ASP.Net中的移动浏览器是否启用了JavaScript

[英]Detecting if JavaScript is enabled on a Mobile browser in ASP.Net

I want to check if a mobile browser has javascript enabled before displaying a page to the user. 我想在向用户显示页面之前检查移动浏览器是否启用了javascript。 The code I've found from my research is: 从研究中发现的代码是:

System.Web.HttpBrowserCapabilities browser = Request.Browser;
Response.Write(browser.EcmaScriptVersion.ToString());

So to ensure that javascript is enabled on a browser, you need to check wheather the returned valued is either equal to or bigger than 1. This works on normal browsers, but when I test it on my phone the returned value is always 0.0, no matter if my javascript is enabled or disabled. 因此,要确保在浏览器上启用javascript,您需要检查是否返回的值等于或大于1。这在正常的浏览器上有效,但是当我在手机上对其进行测试时,返回的值始终为0.0,否与我的JavaScript是否启用无关。

Is there a way to check if javascript is enabled on a mobile browser or will the browser handle the incapability on it's own? 有没有一种方法可以检查是否在移动浏览器上启用了javascript,或者浏览器将自行处理此功能?

I had the exact same problem myself. 我本人也有同样的问题。 We ended up writing a small piece of javascript that would set the value of a hidden variable in a form to 1, and post back to the server right away on a welcome page, and then stored this hint in the session. 我们最终编写了一小段javascript,它将表单中的隐藏变量的值设置为1,并立即在欢迎页面上发回到服务器,然后将此提示存储在会话中。

its not fantastic, but between all the different mobile browsers, and the different issues we found with them, it was the only one we could reply upon 并不是很好,但是在所有不同的移动浏览器之间,以及我们在浏览器中发现的不同问题之间,这是我们唯一可以回答的问题

If you want to use ASP.NET to try and determine this, make sure you are using the latest version of the Mobile Device Browser File . 如果要使用ASP.NET尝试确定此问题,请确保使用的是最新版本的“ 移动设备浏览器文件” Without this, ASP.NET has very limited support for detecting mobile browsers. 没有这个,ASP.NET对检测移动浏览器的支持非常有限。

However, the only reliable way to detect for browser support is to do it on the page (in the browser). 但是,检测浏览器支持的唯一可靠方法是在页面上(在浏览器中)执行此操作。 THe best the server could tell would be if the browser has support for JavaScript but would not know if the user had turned this off in browser settings. 服务器最好的判断是浏览器是否支持JavaScript,但不知道用户是否在浏览器设置中将其关闭。

When implementing for the mobile web you'll also need to be aware that different browsers don't implement all JavaScritp functionality. 在为移动网络实现时,您还需要注意,不同的浏览器并不能实现所有JavaScritp功能。 This means that simply deteccting javascript may not be enough. 这意味着仅仅检测javascript可能还不够。 You need to test for the individual functionality you're after. 您需要测试所需的单个功能。 This definitely can't be done by any currently available server side device capability database. 绝对不能通过任何当前可用的服务器端设备功能数据库来完成此操作。

Look to use progressive enhancement when it come to JavaScript on mobile web pages. 当涉及移动网页上的JavaScript时,请使用渐进增强。 This will help ensure that your site will be usable by more people. 这将有助于确保您的网站可以被更多的人使用。

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

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