Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Monday, April 23, 2012
Using HTML comment tag <!— --> still relevant around JavaScript code?
Is it still relevant to use HTML comment tag around JavaScript code?
HTML comments, ie. <!-- -->, are no longer needed. They were intended to allow browsers that didn't understand the <script> tag to degrade gracefully. These browsers, eg. Netscape 1.x are no longer found in the wild. So there is really no point in putting HTML comments in your script tags anymore.
If you want your HTML to validate as XHTML or XML, you probably want to use a commented out CDATA tag.
The reason for this is so your <, >, &, " and ' that are part of your javascript code won't have to be encoded as <, >, &, " and ' respectively.
It is better to just avoid JavaScript in the body all together. It makes things easier to update, avoids the needs for comments and forces you to plan for non-JavaScript enabled users as well as users with JavaScript enabled.
The tags are valid for XML based markup including HTML. Javascript uses a c-style syntax so the commenting should be done with "//" or "/* comment */" for blocks.
HTML comments, ie. <!-- -->, are no longer needed. They were intended to allow browsers that didn't understand the <script> tag to degrade gracefully. These browsers, eg. Netscape 1.x are no longer found in the wild. So there is really no point in putting HTML comments in your script tags anymore.
ReplyDeleteIf you want your HTML to validate as XHTML or XML, you probably want to use a commented out CDATA tag.
<script type="text/javascript">
//<![CDATA[
document.write("Hello World!");
//]]>
</script>
The reason for this is so your <, >, &, " and ' that are part of your javascript code won't have to be encoded as <, >, &, " and ' respectively.
Not really, unless you're targeting 15-year-old browsers.
ReplyDeleteIt is better to just avoid JavaScript in the body all together. It makes things easier to update, avoids the needs for comments and forces you to plan for non-JavaScript enabled users as well as users with JavaScript enabled.
ReplyDeleteThe tags are valid for XML based markup including HTML. Javascript uses a c-style syntax so the commenting should be done with "//" or "/* comment */" for blocks.
ReplyDelete