5,317,598 members and growing! (24,453 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Samples     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
C# (C# 1.0, C# 2.0, C# 3.0, C#), Javascript, .NET (.NET, .NET 3.5, .NET 3.0, .NET 1.0, .NET 1.1, .NET 2.0), ASP.NET

Posted: 22 Feb 2008
Updated: 22 Feb 2008
Views: 7,267
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
9 votes for this Article.
Popularity: 3.70 Rating: 3.88 out of 5
2 votes, 22.2%
1
1 vote, 11.1%
2
0 votes, 0.0%
3
1 vote, 11.1%
4
5 votes, 55.6%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

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

Background

I had 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 4 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
    {
        Session["JSChecked"] = "Checked";    // prevent infinite loop
        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).

History

License

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

About the Author

ShawnDevin


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
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralAlternative to check javascript support without roundtrip [modified]memberRed Feet23:29 25 Feb '08  
GeneralRe: Alternative to check javascript support without roundtripmemberShawnDevin11:17 26 Feb '08  
GeneralWhy not Request.Browser.JavaScript ??memberDileep.M19:34 25 Feb '08  
GeneralRe: Why not Request.Browser.JavaScript ??memberAdam Tibi23:21 25 Feb '08  
GeneralRe: Why not Request.Browser.JavaScript ??memberDileep.M23:45 25 Feb '08  
GeneralRe: Why not Request.Browser.JavaScript ??memberShawnDevin11:14 26 Feb '08  
Generalthanksmemberbinabic7:47 23 Feb '08  
GeneralHere is another way!memberazamsharp6:36 23 Feb '08  
GeneralRe: Here is another way!memberRed Feet23:16 25 Feb '08  
GeneralThanksmvpJeffrey Walton15:43 22 Feb '08  

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

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