Click here to Skip to main content
13,554,127 members
Click here to Skip to main content
Add your own
alternative version


41 bookmarked
Posted 22 Feb 2008
Licenced CPOL

Detect if JavaScript is enabled in ASPX

, 22 Feb 2008
Rate this:
Please Sign up or sign in to vote.
Check if the browser has JavaScript enabled in the Page_Load method.


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


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.");
        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='default.aspx?JScript=1';", true); 

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


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


About the Author

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).

You may also be interested in...

Comments and Discussions

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180515.1 | Last Updated 22 Feb 2008
Article Copyright 2008 by ShawnDevin
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid