11

在 ASP.NET Web 控件的 Render 方法中,我需要根据客户端浏览器上是否启用或禁用 JavaScript 来更改 Html 的输出,

有谁知道正确的咒语来解决这个问题?

4

5 回答 5

13

使用脚本检查是否启用了 javascript 的问题在于,您只有在脚本未运行后才能发现。

一些解决方案尝试相反 - 他们使用 javascript 设置一个值,然后在以后检测到该值时提供启用 Javascript 的控件。但是,对于 Firefox NoScript 插件等有时启用 javascript 的工具,这会失败。

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

但是我不知道这与 ASP.NET 方法对控件的影响如何。

于 2008-10-29T11:47:38.443 回答
3

如果不执行脚本,则noscript标记用于定义替代内容(文本)。

编辑:嗨 Kieron,看看这个链接:为 JavaScript 测试创建服务器控件,并检测是否在 ASPX 中启用了 JavaScript

于 2008-10-29T11:39:14.017 回答
3

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.

于 2008-10-29T11:49:33.683 回答
0

这是一种检查何时提交表单的方法。

http://www.4guysfromrolla.com/webtech/082400-1.shtml

我认为没有任何 Request 属性会告诉您何时首次请求该页面。有一个属性会告诉您浏览器是否支持 JS,但如果启用则不会。看这里

于 2008-10-29T11:48:46.180 回答
0

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

于 2017-12-27T16:52:46.493 回答