Skip to main content
Email Password   helpLost your password?

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralReally helpful, but one suggestion Pin
PGumbo
14:38 27 Feb '09  
GeneralWhy Response.Write method fails in FireFox Pin
JamieS
9:44 25 Feb '09  
GeneralAlternative to check javascript support without roundtrip [modified] Pin
Red Feet
23:29 25 Feb '08  
GeneralRe: Alternative to check javascript support without roundtrip Pin
ShawnDevin
11:17 26 Feb '08  
GeneralWhy not Request.Browser.JavaScript ?? Pin
Dileep.M
19:34 25 Feb '08  
GeneralRe: Why not Request.Browser.JavaScript ?? Pin
Adam Tibi
23:21 25 Feb '08  
GeneralRe: Why not Request.Browser.JavaScript ?? Pin
Dileep.M
23:45 25 Feb '08  
GeneralRe: Why not Request.Browser.JavaScript ?? Pin
ShawnDevin
11:14 26 Feb '08  
Generalthanks Pin
binabic
7:47 23 Feb '08  
GeneralHere is another way! Pin
azamsharp
6:36 23 Feb '08  
GeneralRe: Here is another way! Pin
Red Feet
23:16 25 Feb '08  
GeneralThanks Pin
Jeffrey Walton
15:43 22 Feb '08  


Last Updated 22 Feb 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009