comments in html

Hi<br />
I've got a question about the use of the <!-- --> comment tags in HTML.<br />
<br />
I've noticed that often these tags get placed around , for example, the code inside of <style> </style> tags. Like so<br />
<br />
<style something or the other><br />
<!--<br />
<br />
style info in here<br />
<br />
--><br />
</style><br />
<br />
I though it might be so that browsers who cant cope with CSS wont see that code. but will any browser see it if it is between those comment tags?<br />
It seems as though my browser does, but I'm not sure. Or is there another kind of comment tag that I'm not aware of?<!--content-->A "well-behaved" browser is supposed to ignore any tag that it does not support. So, technically, you should not need to place HTML comments around style or script statements.<br />
<br />
Some older browsers (I don't even remember the versions - 20 years is a long time) had problems with script, especially in the BODY section. I do not believe that is the case anymore. I have never heard of any problems with styles. I think it has just crept into the culture.<!--content-->Particularly old browsers (version 3 I think) have a tendancy to print the code onto the screen if it's not commented out.<!--content-->I did some more research on this and found this<br />
<!-- m --><a class="postlink" href="http://hotwired.lycos.com/webmonkey/98/03/comment_trick.html">http://hotwired.lycos.com/webmonkey/98/ ... trick.html</a><!-- m --><br />
<br />
Which basically says this<br />
<br />
Why the comment trick works: <br />
Here's the trick again: <br />
<br />
<html> <br />
<head> <br />
<title> blah blah blah </title> <br />
<script language="JavaScript"> <br />
<br />
<!-- hide this stuff from other browsers <br />
<br />
YOUR SCRIPT HERE <br />
<br />
// end the hiding comment --> <br />
<br />
</script> </head> <br />
<body><br />
etc, etc, etc. <br />
<br />
And here's why it works: <br />
<br />
Normally, everything between <!-- and --> in an HTML page isn't displayed. This part of HTML works to hide JavaScript code from browsers that don't support JavaScript. <br />
<br />
However, the clever people who invented JavaScript decided to make the <!-- tag mark the beginning of a one-line comment instead of the beginning of a multiline comment block (like it is in HTML). Because of this, the JavaScript interpreter will skip over that comment line (if it occurs between script tags) and happily continue processing the rest of your script. <br />
<br />
Unfortunately, JavaScript doesn't know enough to ignore the closing comment tag (-->), so you have to comment it out by sticking the // at the beginning of the "end hiding comment" text. If you don't, JavaScript will try to interpret the --> as a JavaScript statement, and you'll get an error. <br />
<br />
It's weird and tricky, but it works. I just do it by memory now. You can cut out the "hide this stuff" and "end hiding comment" text. All that matters is the <!-- on the line after the <script> tag and the // --> on the line before the </script> tag.<!--content-->You realise you've jumped between style and script tags? :)<!--content-->Oh. <br />
So I have. Comes from trying to learn lots of different things at the same time.<br />
<br />
So - does the above hold true for style tags too? And what is the best method to use in either case, for style tags and script tags?<!--content-->Use external files for both:<br />
<link rel="StyleSheet" href=http://www.webdeveloper.com/forum/archive/index.php/"myStyles.css" type="text/css" /><br />
<script type="text/javascript" src=http://www.webdeveloper.com/forum/archive/index.php/"myJavaScript.js"></script><!--content-->I agree. External is always best because it means it only needs to be Download <!--more-->ed once. It can be reused for every page of the site.<br />
<br />
It's probably similar (the reason for the need for comments). My understanding is that old browsers with poor/non-existent CSS support might print the css onto the screen, as they don't know what to do with it - so similar for the script tags.<!--content-->You can use the @ import (<!-- m --><a class="postlink" href="http://centricle.com/ref/css/filters/">http://centricle.com/ref/css/filters/</a><!-- m -->) rule to prevent older browsers from loading the style sheet.<!--content-->
 
Back
Top