Click here to Skip to main content
Licence CPOL
First Posted 22 Feb 2008
Views 55,084
Downloads 360
Bookmarked 35 times

Detect if JavaScript is enabled in ASPX

By | 22 Feb 2008 | Article
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)

About the Author

ShawnDevin

Web Developer
RFP Express
United States United States

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralWorks fine Pinmemberiman khalil3:49 31 May '12  
QuestionThanks... please have a look at this though? PinmemberGinozzzz6:22 11 May '10  
GeneralReally helpful, but one suggestion PinmemberPGumbo13:38 27 Feb '09  
This is really helpful, thank you. The one thing I noticed is that this will go into an infinite loop if javascript is enabled but cookies are disabled, so you may want to check for the querystring as well as the session variable, like:
 
if (Session["JSChecked"] == null && Request.QueryString["JScript"] == null)
GeneralWhy Response.Write method fails in FireFox PinmemberJamieS8:44 25 Feb '09  
GeneralRe: Why Response.Write method fails in FireFox PinmemberBrianBissell9:10 3 Aug '11  
GeneralAlternative to check javascript support without roundtrip [modified] PinmemberRed Feet22:29 25 Feb '08  
GeneralRe: Alternative to check javascript support without roundtrip PinmemberShawnDevin10:17 26 Feb '08  
GeneralRe: Alternative to check javascript support without roundtrip PinmemberBrianBissell9:13 3 Aug '11  
QuestionWhy not Request.Browser.JavaScript ?? PinmemberDileep.M18:34 25 Feb '08  
AnswerRe: Why not Request.Browser.JavaScript ?? PinmemberAdam Tibi22:21 25 Feb '08  
GeneralRe: Why not Request.Browser.JavaScript ?? PinmemberDileep.M22:45 25 Feb '08  
GeneralRe: Why not Request.Browser.JavaScript ?? PinmemberShawnDevin10:14 26 Feb '08  
Generalthanks Pinmemberbinabic6:47 23 Feb '08  
GeneralHere is another way! Pinmemberazamsharp5:36 23 Feb '08  
GeneralRe: Here is another way! PinmemberRed Feet22:16 25 Feb '08  
GeneralThanks PinmvpJeffrey Walton14:43 22 Feb '08  

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.

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