Introduction
Figuring out if the client browser is capable of running JavaScript is easy (Request.Browser.JavaScript
), but figuring out if the client has this feature turned on in his/her browser becomes a bit more tricky.
Background
I have experimented with numerous ways, and found this to be the most efficient (free) method of detecting JavaScript active status on the client browser.
Using the code
The code is available in the JSDetect.cs page (the page that will be doing the detection of the JavaScript status). I have added in comments to explain the behavior:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
namespace JSDetect
{
public partial class JSDetect : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session["jsActive"] = 0;
ScriptManager.RegisterStartupScript(this.Page, this.GetType(),
"JSDetect", "PageMethods.SetJSEnabled();", true);
refreshCommand.Content = "2; url=" +
Request.QueryString["url"].ToString();
}
[WebMethod]
[System.Web.Script.Services.ScriptMethod()]
public static void SetJSEnabled()
{
HttpContext.Current.Session["jsActive"] = 1;
HttpContext.Current.Response.Redirect(
HttpContext.Current.Request.QueryString["url"].ToString());
}
}
}
In the JSDetect.aspx page, there is a meta refresh tag in the header to redirect to the caller page if nothing has happened. The script manager added to the form
tag needs to have the EnablePageMethods
property set to true
in order to run the Web Method.
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="JSDetect.aspx.cs" Inherits="JSDetect.JSDetect" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title></title>
<meta id="refreshCommand" runat="server"
HTTP-EQUIV="REFRESH" content="">
</head>
<body>
<form id="jsTest" runat="server">
<asp:ScriptManager ID="smanjs"
runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<div id="ContentDiv" runat="server">
</div>
</form>
</body>
</html>
Now, to call this page in your application page, as you can see, the session is being referenced. If it does not exist, it goes and does the check. This is good in the case of sessions timing out. Also, I added a bit to skip this step for search engines.
I made the method for detection public static
with HttpContext
classes to ease the movement of this method into an external library.
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;
namespace JSDetect
{
public partial class _Default : System.Web.UI.Page
{
bool jsEnabled = false;
protected void Page_Load(object sender, EventArgs e)
{
jsEnabled = IsJavascriptActive();
Response.Write("Javascript running in browser: " + jsEnabled);
}
public static bool IsJavascriptActive()
{
bool active = false;
HttpContext context = HttpContext.Current;
if (!context.Request.Browser.Crawler)
{
if (context.Session["jsActive"] == null)
{
context.Response.Redirect(ClientDomainName() +
"/JSDetect.aspx?url=" +
context.Request.Url.AbsoluteUri.ToString() +
" ", true);
}
else
{
if (context.Session["jsActive"].ToString().Equals("0"))
{
active = false;
}
else if (context.Session["jsActive"].ToString().Equals("1"))
{
active = true;
}
}
}
return active;
}
public static string ClientDomainName()
{
string domainNameAndPort =
HttpContext.Current.Request.Url.AbsoluteUri.Substring(0,
HttpContext.Current.Request.Url.AbsoluteUri.Length -
HttpContext.Current.Request.Url.PathAndQuery.Length);
return domainNameAndPort;
}
}
}
Points of interest
I felt I could kick myself for not thinking of this before since I have done something very similar with AJAX. I had knowledge of Java / server calls after studying code from the Dropthings.com drag drop service.
This can also be embedded into your default page without using an additional page, but this was built with the purpose of reusing the method.