检测 ASP.NET 中何时禁用 JavaScript
在 ASP.NET Web 控件的 Render 方法中,我需要根据客户端浏览器上是否启用或禁用 JavaScript 来更改 Html 的输出,
有谁知道正确的咒语来解决这个问题?
In the Render method of an ASP.NET web-control, I need to alter the output of the Html based on whether JavaScript is enabled or disabled on the clients browser,
Does anyone know the right incantation to figure that out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用脚本检查 javascript 是否启用的问题是,只有在脚本未运行后才能发现。
一些解决方案尝试相反的方法 - 它们使用 javascript 设置一个值,然后在稍后检测到该值时提供启用 Javascript 的控件。 但是,对于有时启用 javascript 的工具(例如 Firefox NoScript 插件),此操作会失败。
更可靠的解决方案是始终发送纯 HTML 兼容控件,然后在页面加载时运行 javascript 以添加正确的事件处理程序/额外的 DOM 元素/等。
但是我不知道这如何与 ASP.NET 控件方法相适应。
The problem with using script to check whether javascript is enabled is that you only find that out after the script hasn't run.
Some solutions try the opposite - they use javascript to set a value and then supply Javascript enabled controls if that value is later detected. However, this fails with javascript-sometimes-enabled tools like the Firefox NoScript plugin.
A more robust solution is to always send the plain-HTML compatible control, and then have javascript run on page load to add the correct event handlers/extra DOM elements/etc.
However I don't know how this fits in with the ASP.NET approach takes to controls.
noscript 标签用于定义替代内容(文本)(如果脚本不是)被执行。
编辑:嗨 Kieron,看看这个链接:创建用于 JavaScript 测试的服务器控件 和 检测 ASPX 中是否启用了 JavaScript
The noscript tag is used to define an alternate content (text) if a script is NOT executed.
EDIT: Hi Kieron, take a look at this link: Creating a Server Control for JavaScript Testing, and Detect if JavaScript is enabled in ASPX
浏览器不会通过请求传达 Javascript 可用性。
我所做的一件事(虽然我不确定它是否适用于您的情况)是生成页面,就像未启用 Javascript 一样,然后使用一些 Javascript 来关闭仅 HTML 的内容并打开 HTML-Javascript东西。
您还可以让首页通过 Javascript 点击“phone home”,以在会话中记录 Javascript 是否启用。
The browser does not communicate Javascript availability with the request.
One thing that I have done, though I'm not sure it is applicable in your case, is generate the page as if Javascript is not enabled, then have some Javascript that turns off the HTML-only stuff and turns on the HTML-Javascript stuff.
You could also have the first page hit "phone home" via Javascript to record in the session whether Javascript is enabled or not.
这是一种检查表单何时提交的方法。
https://web. archive.org/web/20210428071600/http://www.4guysfromrolla.com/webtech/082400-1.shtml
我认为没有任何 Request 属性可以告诉您页面何时被首次请求。 有一个属性会告诉你浏览器是否支持 JS,但不会告诉你是否启用。 参见此处
This is a way to check when a form is being submitted.
https://web.archive.org/web/20210428071600/http://www.4guysfromrolla.com/webtech/082400-1.shtml
I dont think there is any Request property that will tell you when the page is first requested. There is a property that will tell you if the browser supports JS, but not if its enabled. See Here
你需要做的就是这个。 经过多次测试和深夜,我决定使用最简单的解决方案。 将标签拖到页面顶部并确保其显示为“在服务器上运行”。 然后,对于 Text 属性,输入 Text="此网站需要 Javascript"。 这应该是最好的答案。 :D
What you need to do is this. After much testing and late nights, I decided to use the most simple solution. Drag a label onto the top of the page and make sure it reads "run at server". Then, for the Text attribute, put Text="This website requires Javascript". That should be the best answer. :D