Asp.Net – 在页面上没有检测到javascript? (更名为标题)

我有一个页面,在TabContainer中显示其所有内容,但如果在浏览器上禁用了javascript,它只显示一个空白页面。

我可以添加一个来显示所有重要的内容,但空白的TabContainer仍然会呈现。

我想在标题中添加一个重定向到同一页面加上?noscript = true,但它不应该是无限的。 我想使用PlaceHolder控件,当当前url没有noscript查询值时,该控件将放置相应的

然后,当存在noscript查询值时,我可以将TabContainer的Visible属性设置为false。

这是正确的方法吗?

您可以使用HttpBrowserCapabilitites类获取有关浏览器的信息,检查JavaScript支持的属性称为EcmaScriptVersion 。 如果版本> = 1,则浏览器支持JavaScript。

我提出了一种有效的方法,对于Asp.Net来说还是一个新手,所以我对其他人的想法很感兴趣。

编辑我通过引入一个临时(仅会话)cookie来记住这个想法,该cookie记住浏览器是否具有NoScript,因此我们不必继续重定向到同一页面,我们只是在下次请求页面时使用该bool值。

我在母版页的头部做了这个:

  

在TabContainer的Visible="<%# !NoJavascript %>"属性中。

然后在CodeBehind中:

 protected bool NoJavascript { get { TempCookie cookie = new TempCookie(); // session only cookie if (cookie.NoJavascript) return true; bool noJavascript = !string.IsNullOrEmpty(Request["noscript"]); if (noJavascript) cookie.NoJavascript = noJavascript; return noJavascript; } } protected string NoScriptPlaceHolder { get { if (NoJavascript) return string.Empty; string url = Request.Url.ToString(); string adv = "?"; if (url.Contains('?')) adv = "&"; string meta = string.Format("", url, adv); return meta; } } 

还有其他想法吗?

未来的想法:如果他们没有启用Cookie,我想一个将noscript查询值传递给每个请求的实用程序也会有所帮助,否则他们会在每个请求中不断重定向到同一页面。

好吧,因为我很彻底,并且不想重复代码,我创建了这个组件,它执行我的其他答案,并检查会话和viewstate以进行先前的检测。

该组件的价值在于它可以在其他页面上使用,并且它可以访问与该组件的其他页面上使用的相同会话/ cookie值。

有什么改进建议吗?

 using System; using System.ComponentModel; using System.Linq; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; namespace AspNetLib.Controls { /* * This component should be placed in the  html tag and not in the body or form. */ [DefaultProperty("Noscript")] [ToolboxData("<{0}:NoJavascript runat=server />")] public class NoJavascript : WebControl { HttpRequest Request = HttpContext.Current.Request; HttpSessionState Session = HttpContext.Current.Session; HttpResponse Response = HttpContext.Current.Response; [Bindable(true)] [Category("Appearance")] [DefaultValue(false)] [Localizable(true)] public bool Noscript { get { bool noscript; // check if we've detected no script before try { noscript = Convert.ToBoolean(Session["js:noscript"] ?? false); if (noscript) return true; } catch { } // if session disabled, catch its error HttpCookie Cookie = Request.Cookies["JavascriptDetectionComponent"]; if (null != Cookie) { noscript = !string.IsNullOrEmpty(Cookie["js:noscript"]); if (noscript) return true; } noscript = Convert.ToBoolean(ViewState["js:noscript"] ?? false); if (noscript) return true; // if we've returned from meta evaluate noscript query setting noscript = !string.IsNullOrEmpty(Request["noscript"]); if (noscript) { SetNoScript(); return true; } return false; } } [Bindable(true)] [Category("Misc")] [DefaultValue("")] [Localizable(true)] public string CookieDomain { get { return Convert.ToString(ViewState["CookieDomain"] ?? string.Empty); } set { ViewState["CookieDomain"] = value; } } private void SetNoScript() { try { Session["js:noscript"] = true; } catch { }// if session disabled, catch its error ViewState["js:noscript"] = true; HttpCookie Cookie = new HttpCookie("JavascriptDetectionComponent"); if (!string.IsNullOrEmpty(CookieDomain)) Cookie.Domain = CookieDomain; Cookie["js:noscript"] = "true"; Response.Cookies.Add(Cookie); } protected override void RenderContents(HtmlTextWriter output) { if (!Noscript) { string url = Request.Url.ToString(); string adv = "?"; if (url.Contains('?')) adv = "&"; string meta = string.Format("", url, adv); output.WriteLine(""); } } } }