简体   繁体   English

检测ASP.NET中禁用JavaScript的时间

[英]Detect when JavaScript is disabled in ASP.NET

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, 在ASP.NET Web控件的Render方法中,我需要根据客户端浏览器上是启用还是禁用JavaScript来更改Html的输出,

Does anyone know the right incantation to figure that out? 有没有人知道正确的咒语来解决这个问题?

The problem with using script to check whether javascript is enabled is that you only find that out after the script hasn't run. 使用脚本检查是否启用了javascript的问题是,只有在脚本未运行后才能找到它。

Some solutions try the opposite - they use javascript to set a value and then supply Javascript enabled controls if that value is later detected. 一些解决方案尝试相反 - 他们使用javascript设置值,然后提供启用Javascript的控件,如果稍后检测到该值。 However, this fails with javascript-sometimes-enabled tools like the Firefox NoScript plugin. 但是,这有点像Firefox NoScript插件这样的javascript有时启用的工具。

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. 一个更强大的解决方案是始终发送纯HTML兼容控件,然后在页面加载时运行javascript以添加正确的事件处理程序/额外的DOM元素/等。

However I don't know how this fits in with the ASP.NET approach takes to controls. 但是我不知道这与ASP.NET方法对控件的适应性如何。

The noscript tag is used to define an alternate content (text) if a script is NOT executed. 如果未执行脚本,则noscript标记用于定义备用内容(文本)。

EDIT: Hi Kieron, take a look at this link: Creating a Server Control for JavaScript Testing , and Detect if JavaScript is enabled in ASPX 编辑:嗨Kieron,看看这个链接: 为JavaScript测试创建一个服务器控件 ,并检测ASPX中是否启用了JavaScript

The browser does not communicate Javascript availability with the request. 浏览器不会将Javascript可用性与请求进行通信。

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. 我做过的一件事,虽然我不确定它是否适用于你的情况,是生成页面好像没有启用Javascript,然后有一些Javascript关闭HTML-only的东西并打开HTML-Javascript东西。

You could also have the first page hit "phone home" via Javascript to record in the session whether Javascript is enabled or not. 您还可以通过Javascript将第一页打到“phone home”,在会话中记录Javascript是否已启用。

This is a way to check when a form is being submitted. 这是一种检查表单提交时间的方法。

http://www.4guysfromrolla.com/webtech/082400-1.shtml 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. 我不认为有任何Request属性会告诉您何时首次请求页面。 There is a property that will tell you if the browser supports JS, but not if its enabled. 有一个属性会告诉您浏览器是否支持JS,但是如果启用了则不会。 See Here 看这里

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". 然后,对于Text属性,输入Text =“此网站需要Javascript”。 That should be the best answer. 那应该是最好的答案。 :D :d

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

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