Click here to Skip to main content
15,867,453 members
Articles / Web Development / ASP.NET

Detect if JavaScript is enabled in ASPX

Rate me:
Please Sign up or sign in to vote.
4.60/5 (17 votes)
22 Feb 2008CPOL 122.3K   41   17
Check if the browser has JavaScript enabled in the Page_Load method.

Introduction

This code sample detects if the current browser currently has JavaScript enabled.

Background

I searched for a way to determine if the user's web browser was running JavaScript, but discovered most samples on the net only detected if the browser was capable of running JavaScript and which version of JavaScript the browser is able to run. It did nothing to detect (at least from C#) whether JavaScript was currently enabled.

Using the code

I finally found four tutorials which I have combined bits of into one simple block of code that can be run from the Page_Load() method to see if JavaScript is enabled on a client's web browser or not.

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (Session["JSChecked"] == null)
    //JSChecked -indicates if it tried to run the javascript version
    {
        // prevent infinite loop
        Session["JSChecked"] = "Checked";
        string path = Request.Url + "?JScript=1";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "redirect", 
          "window.location.href='" + path + "';", true);
    }
    if (Request.QueryString["JScript"] == null)
        Response.Write("JavaScript is not enabled.");
    else
        Response.Write("JavaScript is enabled.");
}

Points of interest

The greatest difficulty was that most tutorials for this type of function on the web all recommend using the following code:

C#
Response.Write(@"<script language="'javascript'" type='text/jscript'>" + 
               @"   window.location  = 'default.aspx?JScript=1'; </script>");

Unfortunately, this code does not work if the browser is Firefox. However, using the command:

C#
Page.ClientScript.RegisterStartupScript(this.GetType(), "redirect", 
           "window.location.href='default.aspx?JScript=1';", true); 

works for all browsers that I have tested (IE, Firefox, Safari, and Opera).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer RFP Express
United States United States
A programmer currently working in C#, SQL, ASPX, and various reporting engines. My favorite program is Photoshop (with a strong fondness for many 3d programs).

Comments and Discussions

 
QuestionSadly not working for me Pin
Member 99942773-Sep-14 1:37
Member 99942773-Sep-14 1:37 
GeneralWorks fine Pin
iman khalil31-May-12 3:49
iman khalil31-May-12 3:49 
QuestionThanks... please have a look at this though? Pin
Ginozzzz11-May-10 6:22
Ginozzzz11-May-10 6:22 
GeneralReally helpful, but one suggestion Pin
PGumbo27-Feb-09 13:38
PGumbo27-Feb-09 13:38 
GeneralWhy Response.Write method fails in FireFox Pin
JamieS25-Feb-09 8:44
JamieS25-Feb-09 8:44 
GeneralRe: Why Response.Write method fails in FireFox Pin
BrianBissell3-Aug-11 9:10
BrianBissell3-Aug-11 9:10 
GeneralAlternative to check javascript support without roundtrip [modified] Pin
Red Feet25-Feb-08 22:29
Red Feet25-Feb-08 22:29 
GeneralRe: Alternative to check javascript support without roundtrip Pin
ShawnDevin26-Feb-08 10:17
ShawnDevin26-Feb-08 10:17 
GeneralRe: Alternative to check javascript support without roundtrip Pin
BrianBissell3-Aug-11 9:13
BrianBissell3-Aug-11 9:13 
QuestionWhy not Request.Browser.JavaScript ?? Pin
Dileep.M25-Feb-08 18:34
Dileep.M25-Feb-08 18:34 
AnswerRe: Why not Request.Browser.JavaScript ?? Pin
Adam Tibi25-Feb-08 22:21
professionalAdam Tibi25-Feb-08 22:21 
GeneralRe: Why not Request.Browser.JavaScript ?? Pin
Dileep.M25-Feb-08 22:45
Dileep.M25-Feb-08 22:45 
GeneralRe: Why not Request.Browser.JavaScript ?? Pin
ShawnDevin26-Feb-08 10:14
ShawnDevin26-Feb-08 10:14 
Generalthanks Pin
binabic23-Feb-08 6:47
binabic23-Feb-08 6:47 
GeneralHere is another way! Pin
azamsharp23-Feb-08 5:36
azamsharp23-Feb-08 5:36 
GeneralRe: Here is another way! Pin
Red Feet25-Feb-08 22:16
Red Feet25-Feb-08 22:16 
GeneralThanks Pin
Jeffrey Walton22-Feb-08 14:43
Jeffrey Walton22-Feb-08 14:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.