Click here to Skip to main content
11,412,451 members (64,459 online)
Click here to Skip to main content

Detect if JavaScript is enabled in ASPX

, 22 Feb 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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:

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:

Page.ClientScript.RegisterStartupScript(this.GetType(), "redirect", 
           "window.location.href="/KB/aspnet/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)

Share

About the Author

ShawnDevin
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 9994277 at 3-Sep-14 2:37
memberMember 99942773-Sep-14 2:37 
GeneralWorks fine Pin
iman khalil at 31-May-12 4:49
memberiman khalil31-May-12 4:49 
QuestionThanks... please have a look at this though? Pin
Ginozzzz at 11-May-10 7:22
memberGinozzzz11-May-10 7:22 
GeneralReally helpful, but one suggestion Pin
PGumbo at 27-Feb-09 14:38
memberPGumbo27-Feb-09 14:38 
GeneralWhy Response.Write method fails in FireFox Pin
JamieS at 25-Feb-09 9:44
memberJamieS25-Feb-09 9:44 
GeneralRe: Why Response.Write method fails in FireFox Pin
BrianBissell at 3-Aug-11 10:10
memberBrianBissell3-Aug-11 10:10 
GeneralAlternative to check javascript support without roundtrip [modified] Pin
Red Feet at 25-Feb-08 23:29
memberRed Feet25-Feb-08 23:29 
GeneralRe: Alternative to check javascript support without roundtrip Pin
ShawnDevin at 26-Feb-08 11:17
memberShawnDevin26-Feb-08 11:17 
GeneralRe: Alternative to check javascript support without roundtrip Pin
BrianBissell at 3-Aug-11 10:13
memberBrianBissell3-Aug-11 10:13 
QuestionWhy not Request.Browser.JavaScript ?? Pin
Dileep.M at 25-Feb-08 19:34
memberDileep.M25-Feb-08 19:34 
AnswerRe: Why not Request.Browser.JavaScript ?? Pin
Adam Tibi at 25-Feb-08 23:21
memberAdam Tibi25-Feb-08 23:21 
GeneralRe: Why not Request.Browser.JavaScript ?? Pin
Dileep.M at 25-Feb-08 23:45
memberDileep.M25-Feb-08 23:45 
GeneralRe: Why not Request.Browser.JavaScript ?? Pin
ShawnDevin at 26-Feb-08 11:14
memberShawnDevin26-Feb-08 11:14 
Generalthanks Pin
binabic at 23-Feb-08 7:47
memberbinabic23-Feb-08 7:47 
GeneralHere is another way! Pin
azamsharp at 23-Feb-08 6:36
memberazamsharp23-Feb-08 6:36 
GeneralRe: Here is another way! Pin
Red Feet at 25-Feb-08 23:16
memberRed Feet25-Feb-08 23:16 
GeneralThanks Pin
Jeffrey Walton at 22-Feb-08 15:43
mvpJeffrey Walton22-Feb-08 15:43 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150427.1 | Last Updated 22 Feb 2008
Article Copyright 2008 by ShawnDevin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid