Click here to Skip to main content
Click here to Skip to main content

Detect if JavaScript is enabled in ASPX

By , 22 Feb 2008
Rate this:
Please Sign up or sign in to vote.

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)

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

 
GeneralWorks fine Pinmemberiman khalil31-May-12 3:49 
QuestionThanks... please have a look at this though? PinmemberGinozzzz11-May-10 6:22 
GeneralReally helpful, but one suggestion PinmemberPGumbo27-Feb-09 13:38 
GeneralWhy Response.Write method fails in FireFox PinmemberJamieS25-Feb-09 8:44 
GeneralRe: Why Response.Write method fails in FireFox PinmemberBrianBissell3-Aug-11 9:10 
GeneralAlternative to check javascript support without roundtrip [modified] PinmemberRed Feet25-Feb-08 22:29 
GeneralRe: Alternative to check javascript support without roundtrip PinmemberShawnDevin26-Feb-08 10:17 
GeneralRe: Alternative to check javascript support without roundtrip PinmemberBrianBissell3-Aug-11 9:13 
QuestionWhy not Request.Browser.JavaScript ?? PinmemberDileep.M25-Feb-08 18:34 
AnswerRe: Why not Request.Browser.JavaScript ?? PinmemberAdam Tibi25-Feb-08 22:21 
GeneralRe: Why not Request.Browser.JavaScript ?? PinmemberDileep.M25-Feb-08 22:45 
GeneralRe: Why not Request.Browser.JavaScript ?? PinmemberShawnDevin26-Feb-08 10:14 
Generalthanks Pinmemberbinabic23-Feb-08 6:47 
GeneralHere is another way! Pinmemberazamsharp23-Feb-08 5:36 
GeneralRe: Here is another way! PinmemberRed Feet25-Feb-08 22:16 
GeneralThanks PinmvpJeffrey Walton22-Feb-08 14: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 | Mobile
Web03 | 2.8.140415.2 | Last Updated 22 Feb 2008
Article Copyright 2008 by ShawnDevin
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid