|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
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
IntroductionHi 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. BackgroundNow 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 CodeEverything 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 InterestIn 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 HistoryLast Update 19/04/2007
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||