Click here to Skip to main content
Click here to Skip to main content
Go to top

JavaScript Detection in ASP.NET

, 12 Apr 2009
Rate this:
Please Sign up or sign in to vote.
Explains how to detect if clients have the JavaScript feature turned on.

Introduction

I have been trying the whole of last week to figure out a way to detect if the client has JavaScript activated on his browser.

Background

I have found a rather useful method for the server side: Request.Browser.JavaScript. This returns a boolean value indicating if the browser is capable of running JavaScript. The problem, however, is that the browser might be capable of running scripts, but it does not mean the user has turned it on yet.

I have tried many techniques, running the JavaScript in the header, changing the property of an HtmlItem, etc., but couldn't get a true/false indicator on the server side, mainly because the server side executes before any JavaScript can change properties. And finally, last night, I thought of another way of doing it:

The Code

Everything is done in the code-behind page, and makes use of cleverly placed Session variables.

protected void Page_Load(object sender, EventArgs e)
{
    //JSChecked -indicates if it tried to run the javascript version
    //JS Works - Inicates whether javascript works or not
 
    if (Session["JSChecked"] == null)
    {
        Session["JSChecked"] = "Checked";
        Response.Write(@"<script language="'javascript'" type" + 
                       @"='text/jscript'>   window.location  " + 
                       @"= 'default.aspx?JScript=1'; </script>");
    }
    //If the javascript Above executed JScript will have a value 
    if (Request.QueryString["JScript"] != null)
    {
        Session["JsWorks"] = "True";
    }
    
    if (Session["JsWorks"] == null)
    // Work under initial assumption that Javascript Doesnt Work
    // On first load
    {
        Session["JsWorks"] = "False";
        //Does Javascript Work
    }
 
    if (Session["JsWorks"].ToString().Equals("False"))
    // Work if JS Is Set to False
    {
        //Load Non Javascript Content
    }
    else
    {
        //Load Javascript enabled content
    }
}

This is a technique to run JavaScript using the code-behind. This will execute at the very top before the <html> tags are loaded.

Response.Write(@"<script language="'javascript'" type='text/jscript'>" + 
               @" window.location = 'default.aspx?JScript=1'; </script>");

Points of Interest

The technique to run JavaScript in the code-behind can also be a very easy way of debugging your application if you use alert(''); methods within the script tag.

Please view my new article on how to achieve this using the Script Manager and a PageMethod call:

History

  • Last update - 19/04/2007.

License

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

Share

About the Author

Chona1171
Web Developer
South Africa South Africa
Hi I am a Software Developer, I have studied, Comprehensive programming, Software Development, Business and Project Management.
 
After my First year of studies I recieved a full bursary for my second year and worked as a Junior Software Development Instructor,
I am skilled in a vast array of languages my top languages being Java (SE,ME,EE),SQL C#, VB.Net, VB 6 & Asp.Net,

Comments and Discussions

 
AnswerUse HTML's NOSCRIPT tag PinmemberHolckS20-Mar-09 11:25 
GeneralRe: Use HTML's NOSCRIPT tag PinmemberChona117121-Mar-09 11:52 
GeneralMy vote of 1 PinmemberMark Nischalke5-Dec-08 7:40 
GeneralUnobstrusive DOM Scripting Pinmemberthe_ox25-Apr-07 21:16 
NewsRequest.Browser.JavaScript Comment [modified] PinmemberChona117124-Apr-07 21:30 
GeneralNice but need this improvement Pinmemberaromr24-Apr-07 2:23 
GeneralRe: Nice but need this improvement PinmemberChona117124-Apr-07 20:49 
GeneralRe: Nice but need this improvement Pinmemberaromr24-Apr-07 22:23 
GeneralRe: Nice but need this improvement PinmemberChona117124-Apr-07 22:37 
GeneralTry this Pinmemberaromr24-Apr-07 22:45 
GeneralRe: Try this PinmemberChona117124-Apr-07 22:51 
GeneralRe: Try this Pinmemberaromr24-Apr-07 22:56 
GeneralRe: Try this PinmemberChona117125-Apr-07 22:41 
GeneralRe: Try this Pinmemberaromr26-Apr-07 3:41 
GeneralRe: Try this PinmemberChona11714-May-07 2:15 
AnswerRe: Try this - mind your language :) Pinmemberjokva1-Sep-07 1:56 
GeneralRe: Try this - mind your language :) Pinmemberjokva1-Sep-07 2:04 
GeneralRe: Try this - mind your language :) PinmemberChona11717-Jan-08 22: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.

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 12 Apr 2009
Article Copyright 2007 by Chona1171
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid