【问题标题】:Asp.Net - Detect no javascript on page? (renamed title)Asp.Net - 在页面上检测到没有 javascript? (更名的标题)
【发布时间】:2011-04-26 02:13:33
【问题描述】:

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

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

我想在标题中添加一个以重定向到同一页面加上 ?noscript=true,但它不应该是无限的。我想使用 PlaceHolder 控件,当当前 url 没有 noscript 查询值时,它将放置适当的<META HTTP-EQUIV="Refresh" CONTENT="0; URL=url?noscript=true">

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

这是正确的做法吗?

【问题讨论】:

    标签: c# asp.net noscript


    【解决方案1】:

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

    【讨论】:

    • 如果用户禁用了javascript,浏览器支持不是一回事还是我错了?
    • 是的,你的权利......对不起......如果你想在JS已经关闭的情况下隐藏一些东西,你可以设置属性dispaly:none;在 CSS 中默认隐藏它。然后在页面中,可以执行一些 JS 来移除属性,使元素可见。这将导致 HTML 被渲染,但除非启用 JS,否则应该完全隐藏它。
    • 这就是我想出这个组件的原因。它允许我检测 JS 是否被禁用并采取相应措施。
    【解决方案2】:

    我想出了一个可行的方法,对 Asp.Net 来说还是很新,所以我对其他人的想法很感兴趣。

    编辑我扩展了这个想法,引入了一个临时(仅限会话)cookie,它会记住浏览器是否有 NoScript,这样我们就不必一直重定向到同一页面,我们只是在下次请求页面时使用该布尔值。

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

    <noscript>
      <%# NoScriptPlaceHolder %>
    </noscript>
    

    在 TabContainer 的 Visible="&lt;%# !NoJavascript %&gt;" 属性中。

    然后在 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("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL={0}{1}noscript=true\">",
                url, adv);
            return meta;
        }
    }
    

    还有其他想法吗?

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

    【讨论】:

      【解决方案3】:

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

      该组件的价值在于它可以在其他页面上使用,并且它可以访问与该组件在其他页面上使用的相同会话/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 <head> 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("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL={0}{1}noscript=true\">",
                      url, adv);
                  output.WriteLine("<noscript>");
                  output.WriteLine("  " + meta);
                  output.WriteLine("</noscript>");
              }
          }
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-06
        • 1970-01-01
        • 2011-11-16
        • 1970-01-01
        • 2011-05-14
        • 1970-01-01
        相关资源
        最近更新 更多