Click here to 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
PGumbo
14: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
JamieS
9:44 25 Feb '09  
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
-----------------------------------------------------------------------------------------------
The reason FireFox fails to run this code is because it does not use understand 'text/jscript'. Replace the type with 'text/javascript' and both FireFox and IE execute the code. (I have not tested other browsers).

Response.Write(@"<script language='javascript' type='text/javascript'>window.location='default.aspx?JScript=1'; </script>");
GeneralAlternative to check javascript support without roundtrip [modified]
Red Feet
23:29 25 Feb '08  
Good idea to make programmers aware of the fact that JavaScript could be disabled or not supported for other reasons (think of WebCrawlers and SearchBots that do consume HTML, but don't support JavaScript).

An alternative way to use your code for checking if the browser supports JavaScript, without the page reloading itself, is to make a link to your detection page like this:

<a href="jsdetect.aspx?JScript=0" onclick="this.href='jsdetect.aspx?JScript=1'" >Next page</a>

You can leave the Session variable out (which consumes a small amount of server RAM per visitor) and check whether the JScript parameter is either 0 (javascriptEnabled=false) or 1 (javascriptEnabled=true)

=================
Red Feet
- internet oplossingen -
=================

<div class="ForumMod">modified on Tuesday, February 26, 2008 4:23 PM</div>
GeneralRe: Alternative to check javascript support without roundtrip
ShawnDevin
11:17 26 Feb '08  
That is an excellent idea. Particularly if I put it on the script for the Login button on the previous web page.
GeneralWhy not Request.Browser.JavaScript ??
Dileep.M
19:34 25 Feb '08  
These are other bst alternatives..(I think)
1.Request.Browser.JavaScript returns boolean.
2.Inbuild <noscript> html tag.

Dileep.M
</noscript>
GeneralRe: Why not Request.Browser.JavaScript ??
Adam Tibi
23:21 25 Feb '08  
I think because this show if the browser "supports" JavaScript or not, it doesn't show if the user has disabled JavaScript.

Make it simple, as simple as possible, but not simpler.

GeneralRe: Why not Request.Browser.JavaScript ??
Dileep.M
23:45 25 Feb '08  
Yes,

http://msdn2.microsoft.com/en-us/library/aa332781(VS.71,printer).aspx

Dileep.M

GeneralRe: Why not Request.Browser.JavaScript ??
ShawnDevin
11:14 26 Feb '08  
Yes, that is exactly why. The same holds true for Request.Browser.EcmaScriptVersion. It tells what version of JavaScript is supported, but not if it is enabled currently.
Generalthanks
binabic
7:47 23 Feb '08  
Simple but useful in every application. Must have. Thx!
GeneralHere is another way!
azamsharp
6:36 23 Feb '08  
Here is another way to check if the client's JavaScript is enabled or not.

http://aspadvice.com/blogs/azamsharp/archive/2007/09/30/Check-If-the-JavaScript-is-Enabled-on-the-Client_2700_s-Browser.aspx[^]

Mohammad Azam
azamsharp@gmail.com
www.KoffeeKoder.com
Houston, TEXAS

GeneralRe: Here is another way!
Red Feet
23:16 25 Feb '08  
Hello Azam,

The article is about detecting JavaScript support in ASPX (Server Side) and having a boolean containing the result of the check: true/false
So you can do something with this information programmatically.

Your solution is (as you mention in the title) in JavaScript (Client Side) and doesn't give a true/false answer on the server side.

But for displaying if JavaScript is supported/enabled to the visitor, your script is perfect since it is independent of the platform and doesn't need a roundtrip.

=================
Red Feet
- internet oplossingen -
=================

GeneralThanks
Jeffrey Walton
15:43 22 Feb '08  
Hi Shawn,

Thanks for the article. I wish all web developers would use it. I'm a System Administrator. I've hardened browsers at many sites (i.e., disable JavaScript, ActiveX, etc in the Internet Zone).

I wish more developers understood security, and programmed accordingly.

Jeff


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