Click here to Skip to main content
15,126,498 members
Articles / Programming Languages / Javascript
Article
Posted 19 Apr 2007

Stats

51K views
23 bookmarked

JavaScript Detection in ASP.NET

Rate me:
Please Sign up or sign in to vote.
1.75/5 (6 votes)
12 Apr 2009CPOL1 min read
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.

C#
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.

C#
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

L Viljoen
Architect
South Africa South Africa
I am a Technical Lead / Architect working on wide array of different technologies and frameworks. I have 2 International Diploma's in Software Development and Information systems and is completing my Bsc in Mathematics and Computer Science.

My Interests and hobbies are robotics , AI, Game development and 3d modelling

Comments and Discussions

 
AnswerUse HTML's NOSCRIPT tag Pin
HolckS20-Mar-09 12:25
MemberHolckS20-Mar-09 12:25 
GeneralRe: Use HTML's NOSCRIPT tag Pin
L Viljoen21-Mar-09 12:52
professionalL Viljoen21-Mar-09 12:52 
GeneralMy vote of 1 Pin
Not Active5-Dec-08 8:40
mentorNot Active5-Dec-08 8:40 
GeneralUnobstrusive DOM Scripting Pin
the_ox25-Apr-07 22:16
Memberthe_ox25-Apr-07 22:16 
NewsRequest.Browser.JavaScript Comment [modified] Pin
L Viljoen24-Apr-07 22:30
professionalL Viljoen24-Apr-07 22:30 
GeneralNice but need this improvement Pin
aromr24-Apr-07 3:23
Memberaromr24-Apr-07 3:23 
Ok, this is nice and simple , but as u may see that whenever user visits ur page for the first time the page will unpredictly reload again. I think this might annoy the users and reduce the efficiency of your appliacation
Instead I think this improvement will help. Specially if you want to apply this to all your pages
1- Make an external page that is responsible for the receiving the requests . it does not need any query string parameters as when ever this page is called the current Session Variable is set.
2- In each client page u will issue call for this page using the XmlHttpRequset . as hidden request ,so that the whole process done seamless .

Good luck


In The Name OF All The Most Merciful

And Bless His Messenger Mohmed The Greatest Human Ever.....

GeneralRe: Nice but need this improvement Pin
L Viljoen24-Apr-07 21:49
professionalL Viljoen24-Apr-07 21:49 
GeneralRe: Nice but need this improvement Pin
aromr24-Apr-07 23:23
Memberaromr24-Apr-07 23:23 
GeneralRe: Nice but need this improvement Pin
L Viljoen24-Apr-07 23:37
professionalL Viljoen24-Apr-07 23:37 
GeneralTry this Pin
aromr24-Apr-07 23:45
Memberaromr24-Apr-07 23:45 
GeneralRe: Try this Pin
L Viljoen24-Apr-07 23:51
professionalL Viljoen24-Apr-07 23:51 
GeneralRe: Try this Pin
aromr24-Apr-07 23:56
Memberaromr24-Apr-07 23:56 
GeneralRe: Try this Pin
L Viljoen25-Apr-07 23:41
professionalL Viljoen25-Apr-07 23:41 
GeneralRe: Try this Pin
aromr26-Apr-07 4:41
Memberaromr26-Apr-07 4:41 
GeneralRe: Try this Pin
L Viljoen4-May-07 3:15
professionalL Viljoen4-May-07 3:15 
AnswerRe: Try this - mind your language :) Pin
jokva1-Sep-07 2:56
Memberjokva1-Sep-07 2:56 
GeneralRe: Try this - mind your language :) Pin
jokva1-Sep-07 3:04
Memberjokva1-Sep-07 3:04 
GeneralRe: Try this - mind your language :) Pin
L Viljoen7-Jan-08 23:08
professionalL Viljoen7-Jan-08 23:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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