Home » Online Computer Education » Learn How to Hiding JavaScript Statements , Lesson 10

Learn How to Hiding JavaScript Statements , Lesson 10

 

 

HTML comments begin with the characters <!– and end with –>. The browser knows not to display anything between these sets of characters. You also know that the HTML <SCRIPT> tag is used to embed JavaScript into HTML pages. Both JavaScript and non-JavaScript browsers know not to display the <SCRIPT> tags.

However, non-JavaScript browsers do not know what to make of the statements within the <SCRIPT> tags, so they will display the actual lines of the script right in the middle of the HTML page. I am sure that this is not what you want your visitors to see. To get around this unsightly mess, you can enclose all the JavaScript statements starting after the beginning <SCRIPT> tag and before the ending </SCRIPT> tag within HTML comments, as demonstrated in the following example:

<HTML>
<HEAD>
<TITLE>Script 2.1 -Example of Comment Statements</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE=”JavaScript” TYPE=”Text/JavaScript”>
<!– Start hiding JavaScript statements
document.write(“Non-JavaScript browsers will not see
this message.”);
// End hiding JavaScript statements –>
</SCRIPT>
</BODY>
</HTML>
Since browsers are smart enough not to process any HTML that they do not understand, the starting and ending <SCRIPT> tags are ignored by non-JavaScript browsers. At the same time, non-JavaScript browsers will view everything between the HTML comments tags as one big comment and ignore it, thus hiding all JavaScript statements and preventing their display.

JavaScript-enabled browsers, on the other hand, will recognize the starting and ending <SCRIPT> tags and process them accordingly. These browsers also will recognize the opening HTML comment tag and ignore it, while simultaneously seeing the line beginning with the // characters as a JavaScript comment and ignoring it as well.

You also can use comments to make your JavaScripts and JScripts self-documenting by using them to add descriptive comments to your scripts.

About

The main objective of this website is to provide quality study material to all students (from 1st to 12th class of any board) irrespective of their background as our motto is “Education for Everyone”. It is also a very good platform for teachers who want to share their valuable knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *