|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
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
IntroductionThis code sample detects if the current browser currently has JavaScript enabled. BackgroundI had 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 codeI finally found 4 tutorials which I have combined bits of into one simple block of code that can be run from thePage_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
{
Session["JSChecked"] = "Checked"; // prevent infinite loop
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 InterestThe 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). History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||