Asp.Net - Detect no javascript on page? (renamed title)
I have a page that displays all its content in a TabContainer, but if java开发者_JAVA百科script is disabled on the browser it just displays a blank page.
I can add a <noscript>
to display all the important content, but the blank TabContainer still renders.
I'd like to add a in the header to redirect to the same page plus ?noscript=true, but it shouldn't be infinite. I figure using a PlaceHolder control that will put the appropriate <META HTTP-EQUIV="Refresh" CONTENT="0; URL=url?noscript=true">
when the current url doesn't have the noscript query value.
Then I can set the Visible property to false for the TabContainer when the noscript query value is present.
Is this the right way to go about it?
You can use HttpBrowserCapabilitites class to obtain information about the browser, the property to check for JavaScript support is called EcmaScriptVersion. If it has a version >= 1, the browser supports JavaScript.
I came up with a method that works, still pretty new to Asp.Net so I'm interested in what others think.
Edit: I expanded on the idea by introducing a temporary (session only) cookie that remembers if the browser has NoScript so we don't have to keep redirecting to the same page, we just use that bool value the next time a page is request.
I did this in the head of the master page:
<noscript>
<%# NoScriptPlaceHolder %>
</noscript>
In the TabContainer's Visible="<%# !NoJavascript %>"
property.
Then in the 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;
}
}
Any other ideas?
Future thought: If they don't have Cookies enabled, I guess a utility to pass the noscript query value to each request would help as well, otherwise they will get continually redirected to the same page on each request.
Well, because I'm thorough, and don't want to duplicate code, I created this component that does my other answer plus checks session and viewstate for previous detection.
The value of the component is that it can be used on other pages and it will have access to the same session/cookie value that was used on other pages with the component.
Any suggestions at improvement?
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>");
}
}
}
}
精彩评论