2011-04-26 25 views
3

我有一個頁面顯示在TabContainer中的所有內容,但是如果在瀏覽器上禁用了JavaScript,它只會顯示一個空白頁面。Asp.Net - 檢測頁面上沒有JavaScript? (更名標題)

我可以添加一個<noscript>來顯示所有重要的內容,但空白的TabContainer仍然呈現。

我想在標題中添加一個重定向到相同的頁面加上?noscript = true,但它不應該是無限的。我使用一個PlaceHolder控件,噹噹前的url沒有noscript查詢值時,它將放置合適的<META HTTP-EQUIV="Refresh" CONTENT="0; URL=url?noscript=true">

然後我可以設置可見屬性爲TabContainer當noscript查詢值存在時爲false。

這是正確的方式嗎?

回答

0

那麼,因爲我是徹底的,不想複製代碼,我創建了這個組件,它做了我的其他答案,再加上檢查會話和視圖狀態以檢測以前的情況。

組件的值是它可以在其他頁面上使用,並且它將有權訪問組件的其他頁面上使用的相同會話/ 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>"); 
     } 
    } 
} 
} 
0

我想出了一個可行的方法,對Asp.Net來說還是很新的,所以我對別人的想法很感興趣。

編輯我擴大的想法通過引入一個臨時的(會話只)的cookie可以記住如果瀏覽器具有NoScript的,所以我們不必保持重定向到同一個頁面,我們只是使用布爾值下一次請求頁面時。

我這樣做是在母版頁的頭:

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

在TabContainer的的Visible="<%# !NoJavascript %>"財產。

然後在代碼隱藏:

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; 
    } 
} 

任何其他的想法?未來的想法:如果他們沒有啓用Cookies,我想一個實用程序將noscript查詢值傳遞給每個請求也會有所幫助,否則他們將不斷重定向到每個請求的同一頁面。

1

您可以使用HttpBrowserCapabilitites類,以獲取有關瀏覽器的信息,來檢查JavaScript支持的屬性被稱爲EcmaScriptVersion。如果版本大於等於1,則瀏覽器支持JavaScript。

+1

如果瀏覽器支持是不是一樣的事情,如果JavaScript被用戶禁用或我錯了嗎? – 2011-04-27 22:19:49

+0

是的,你的權利...對不起...如果你想隱藏JS關閉的東西,你可以設置屬性顯示:無;在CSS中默認隱藏它。然後在頁面中,您可以執行一些JS來刪除屬性以使該元素可見。這將導致HTML呈現,但應該完全隱藏,除非啓用JS。 – Zachary 2011-04-27 22:44:26

+0

這就是爲什麼我想出了這個組件。它允許我檢測JS是否被禁用並採取相應措施。 – 2011-04-28 00:01:40

相關問題