Wednesday, October 21, 2009

Javascript: How to check if javascript Enabled or Disabled in all browser

This script works in any browser that supports javascript. No PHP or special scripting is involved.

The only scripting languages used in this script are Javascript and HTML. The script is a fairly short script, but it will do the job.

It will be easier to understand this script if you have a simple knowledge of HTML and javascript. If you don't you can still just copy and paste into your website.

This script is free to all, and it can be edited, reposted, or redistributed.
By taking/using this script, you understand that I am not responsible for any problems that may occur.

Use the example below to do this in your website. First, make a file with a file extension that supports HTML and Javascript (such as .html OR .php). Remember to include the "onLoad" portion of the body tag.



<html>
<head>

<title>Soeren Walls - How to Check if Javascript is Enabled</title>

<script type="text/javascript">
function checkForJavascript() {
   /* use this code to only display a message when javascript is disabled */
   document.getElementById('divJavascriptDisabled').style.display = "none";
   /* use this code to show different messages based on whether Javascript is enabled or not */
   document.getElementById('divJavascriptChange').innerHTML = "Javascript is enabled.";
   }
</script>

</head>

<!--
**** REMEMBER to include the "onLoad" portion of the body tag. It is vital to the code.
-->

<body onLoad="checkForJavascript();" style="font-family: Trebuchet MS, Lucida Grande, Verdana, Arial, Sans-Serif; font-size: 12px;">

<p>
Script written by Soeren Walls.<br />
You may remove this message if you would like to. This script is free to all and can be edited, reposted, or redistributed.<br />
By taking/using this script, you understand that I am not responsible for any problems that may occur.<br />
This script works in any browser that supports javascript. No PHP or special scripting is involved.<br />
__________________________________________________________________________________________________
</p>
<p>
If javascript is disabled, you will see a message below. If it's enabled, no message will appear.
</p>

<div align="left" id="divJavascriptDisabled" style="width: 300px; display: block; background-color: #005263; border: 1px dashed #000; padding: 3px; color: #FFFFFF; text-align: center;">
Javascript is disabled. Please enable javscript.
</div>

<p>
__________________________________________________________________________________________________
<br />
Or, if you would like to display different text depending on whether Javascript is enabled or disabled, then use the following script.
</p>

<div align="left" id="divJavascriptChange" style="width: 300px; display: block; background-color: #005263; border: 1px dashed #000; padding: 3px; color: #FFFFFF; text-align: center;">
Javascript is disabled. Please enable javscript.
</div>

</body>
</html>

No comments:

Post a Comment

Popular Hybrid Mobile App development frameworks

Popular Hybrid Mobile App development frameworks Hybrid Development Technologies List of best Hybrid App Development Frameworks ...