Click here to Skip to main content
6,822,123 members and growing! (17,325 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Howto     Beginner License: The Code Project Open License (CPOL)

Detect if JavaScript is enabled in ASPX

By ShawnDevin

Check if the browser has JavaScript enabled in the Page_Load method.
Javascript, C#1.0, C#2.0, C#3.0.NET1.0, .NET1.1, .NET2.0, .NET3.0, .NET3.5, ASP.NET, Dev
Revision:2 (See All)
Posted:22 Feb 2008
Views:30,237
Bookmarked:29 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 3.94 Rating: 3.94 out of 5
2 votes, 20.0%
1
1 vote, 10.0%
2

3
1 vote, 10.0%
4
6 votes, 60.0%
5

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='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


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).
Occupation: Web Developer
Company: RFP Express
Location: United States United States

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralReally helpful, but one suggestion PinmemberPGumbo14:38 27 Feb '09  
GeneralWhy Response.Write method fails in FireFox PinmemberJamieS9:44 25 Feb '09  
GeneralAlternative to check javascript support without roundtrip [modified] PinmemberRed Feet23:29 25 Feb '08  
GeneralRe: Alternative to check javascript support without roundtrip PinmemberShawnDevin11:17 26 Feb '08  
GeneralWhy not Request.Browser.JavaScript ?? PinmemberDileep.M19:34 25 Feb '08  
GeneralRe: Why not Request.Browser.JavaScript ?? PinmemberAdam Tibi23:21 25 Feb '08  
GeneralRe: Why not Request.Browser.JavaScript ?? PinmemberDileep.M23:45 25 Feb '08  
GeneralRe: Why not Request.Browser.JavaScript ?? PinmemberShawnDevin11:14 26 Feb '08  
Generalthanks Pinmemberbinabic7:47 23 Feb '08  
GeneralHere is another way! Pinmemberazamsharp6:36 23 Feb '08  
GeneralRe: Here is another way! PinmemberRed Feet23:16 25 Feb '08  
GeneralThanks PinmvpJeffrey Walton15:43 22 Feb '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 22 Feb 2008
Editor: Smitha Vijayan
Copyright 2008 by ShawnDevin
Everything else Copyright © CodeProject, 1999-2010
Web18 | Advertise on the Code Project