Some browsers do not support Javascript

Browsers that do not support Javascript, will display Javascript  as page content.

To prevent them from doing this and as a part of the Javascript standard the HTML comment tag should be used to "hide" the javascript.

Just add an HTML comment tag <!-- before the first Javascript statement and a --> (end of comment) after the last Javascript statement, like this:

<html>
<body>
<script type="text/javascript">
<!--
document.getElementById("demo").innerHTML=Date();
//-->
</script>
</body>
</html>

The two forward slashes at the end of the comment line (//) is the Javascript comment symbol. This prevents Javascript from executing the --> tag.

Warning for non-Javascript browsers:

If you have to do something important using Javascript then you can display a warning message to the user using <noscript> tags.

You can add a noscript block immediately after the script block as follows:

<html>
<body>

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

document.write("Hello World!")

//-->
</script>

<noscript>
Sorry...Javascript is needed to go ahead.
</noscript>
</body>
</html>

Now, if user's browser does not support Javascript or Javascript is not enabled then message from </noscript> will be displayed on the screen.

Semicolons are optional:

Simple statements in javascript are generally followed by a semicolon character, just as they are in C,C++, and Java. Javascript, however allows you to omit this semicolon if your statements are each placed on a separate line. For example, the following code could be written without semicolons.

<script language="javascript" type="text/javascsript">
<!-- 

var1 = 10
var2 = 20
//-->
</script>

But when formatted in a single line as follows, the semicolons are required:

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

var1 = 10; var2 = 20;

//-->
</script>

Note: It is a good programming practice to use semicolons.

Related Posts

Post a Comment

0 Comments