5,702,921 members and growing! (22,200 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate

JavaScript Detection on ASP.NET

By Chona1171

Explains how to detect if clients have javascript turned on
Javascript, C# 2.0, C#, Windows, .NET, .NET 2.0VS2005, Visual Studio, Dev

Posted: 19 Apr 2007
Updated: 19 Apr 2007
Views: 19,961
Bookmarked: 14 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 1.40 Rating: 2.00 out of 5
3 votes, 60.0%
1
1 vote, 20.0%
2
1 vote, 20.0%
3
0 votes, 0.0%
4
0 votes, 0.0%
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

Hi to all , I have been sitting the last week trying to figure out a way to detect if the client PC has javascript activated on his browser.

Background

Now I have found a rather usefull method for the server side "Request.Browser.JavaScript" this returns a Boolean value indicating if the browser is capable of running javascripts, the problem is however that the browser might be capable of running scripts, but it doesnt mean the user have turned it on yet.

I have tried many techniques , running the javascript in the header changing the property of a htmlItem, but couldn't get the true, false indicator to the server side, mainly beacuse the server side executes before any javascript can change the property. And finally last night I though 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 behindThis 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

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

History

Last Update 19/04/2007

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Chona1171


Hi I am a Software Development Graduate, 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 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,

I worked for CI-Shurlok Engineering as a Progduction Management Administrator and Sftware Developer, and started working for PCW New Media in Feb 2007

My home lanagauge is Afrikaans but everyone at work speaks english so it is rubbing off.

I can aslo brew the strongest pineapple beer in the world. Big Grin but always loose when playing drinking games...
Occupation: Web Developer
Location: South Africa South Africa

Other popular Client side scripting 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 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
GeneralUnobstrusive DOM Scriptingmemberthe_ox22:16 25 Apr '07  
NewsRequest.Browser.JavaScript Comment [modified]memberChona117122:30 24 Apr '07  
GeneralNice but need this improvementmemberaromr3:23 24 Apr '07  
GeneralRe: Nice but need this improvementmemberChona117121:49 24 Apr '07  
GeneralRe: Nice but need this improvementmemberaromr23:23 24 Apr '07  
GeneralRe: Nice but need this improvementmemberChona117123:37 24 Apr '07  
GeneralTry thismemberaromr23:45 24 Apr '07  
GeneralRe: Try thismemberChona117123:51 24 Apr '07  
GeneralRe: Try thismemberaromr23:56 24 Apr '07  
GeneralRe: Try thismemberChona117123:41 25 Apr '07  
GeneralRe: Try thismemberaromr4:41 26 Apr '07  
GeneralRe: Try thismemberChona11713:15 4 May '07  
AnswerRe: Try this - mind your language :)memberjokva2:56 1 Sep '07  
GeneralRe: Try this - mind your language :)memberjokva3:04 1 Sep '07  
GeneralRe: Try this - mind your language :)memberChona117123:08 7 Jan '08  

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

PermaLink | Privacy | Terms of Use
Last Updated: 19 Apr 2007
Editor:
Copyright 2007 by Chona1171
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project