Welcome to AspAdvice Sign in | Join | Help

AzamSharp

Some day I will know everything I hope that day never comes

Syndication

Tags

Navigation

Check If the JavaScript is Enabled on the Client's Browser

If our application is JavaScript intensive then it is always a good idea to check that if the client's machine has JavaScript enabled. When checking for JavaScript you might try some code like the following.

bool isJavaScriptEnabled = Request.Browser.JavaScript

The code above will only confirm that the browser is able to handle the JavaScript or not. This has nothing to do with whether the user has enabled or disabled the JavaScript or not. Actually, Request.Browser.JavaScript has been deprecated in ASP.NET 2.0. You can use the Request.Browser.EcmaScriptVersion.Major >= 1 to check whether the browser supports the JavaScript or not.

So, the question is how do we check if the user has JavaScript enabled?

We will use JavaScript to solve this problem. The part of the idea discussed here was proposed by Paul Weston (http://forums.asp.net/t/1164454.aspx). Create two divs just like shown below:

<div id="jsEnabled" style="visibility:hidden">

JavaScript is enabled

</div>

<div id="jsDisabled">

JavaScript is disabled

</div>

The first div contains the text "JavaScript is enabled" while the second one says "JavaScript is disabled". The first div is also made hidden. Now, we attach the checkJavaScriptValidity function to the onload event of the page.

<body onload="checkJavaScriptValidity()">

checkJavaScriptValidity hides the second div and make the first one active. If JavaScript is enabled you will see the first div which says "JavaScript is enabled". If JavaScript is disabled you will see the second div which says "JavaScript is disabled".

<script language="javascript" type="text/javascript">

function checkJavaScriptValidity()

{

document.getElementById("jsEnabled").style.visibility = 'visible';

document.getElementById("jsDisabled").style.visibility = 'hidden';

}

</script>

 

Sponsor

Published Sunday, September 30, 2007 6:07 PM by azamsharp

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below