HTML comments break down - html

I have a page that is generated which inserts an HTML comment near the top of the page. Inside the comment is a *nix-style command.
<!-- command --option value --option2 value2 --option3 -->
This comment breaks the page completely. What is wrong with the comment to cause this to happen, and why is this the case?

Comments in the XML Spec from the w3.org :
For compatibility, the string "--"
(double-hyphen) MUST NOT occur within
comments.

If you really want to keep the comment in your page you could use this instead of an HTML comment:
<div style="display:none">command --option value --option2 value2 --option3 </div>
Or even
<div class="comment">command --option value --option2 value2 --option3 </div>
and specify:
.comment {display:none;}
in your stylesheet.

Comments at the top of the page before <html> will throw IE into quirks mode, which could explain why the page breaks, if that's where your comment appears.
For more information, check out the "Triggering different rendering modes" on this wikipedia page

Related

IE11 thinks the double dashes in commented code is the end comment tag

In IE11 I'm getting the error Unexpected character in comment end. Expected "-->". because the commented code contains a double dash in the class. Does anyone know how to prevent this from happening?
Example:
<!--
<div class="block--attribute"></div>
-->
From this example I'm guessing IE11 thinks the end of the comment is --a
I have reproduced the warning on my side, this is an HTML5 parser warning, this warning can occur as part of the validation that is performed during HTML parsing. These warnings don't necessarily mean a page is broken, but that the provided HTML is invalid per the HTML5 standard.
When using the comment tag, it does not support any standard attributes (such as the class attribute). More information about Standard Attributes.
To solve this warning, you could remove the class attribute, code as below:
<!--
<div ></div>
-->

JSP comment is called hide comment whereas html comment is called output comment

I'm preparing for an interview so while reading some questions I found that the "JSP comment is called hide comment whereas html comment is called output comment". And the answer says that if we try to view source in JSP, the comments will not be shown whereas in HTML, it will be shown.
But I'm able to see the comment in the view source section.
Can anybody clear this doubt?
Thanks in advance.
There are two types of comments are allowed in the JSP. These are hidden and output comments. A hidden comments does not appear in the generated output in the html, while output comments appear in the generated output.
Example of hidden comment:
<%-- This is hidden comment --%>
Example of output comment:
<!-- This is output comment -->
Well, technically if you use a typical JSP comments , like
<%-- This is a comment --%>
, and do a veiw source , you wont see it on the client side , if you however do an HTML style comment
<!-- This is a comment -->
, you can see view source and see that on the client side . needless to say , you can comment like the normal java style too , inside the jsp tags . hope it helps .
Html comments don't prevent jsp from evaluating values
<!-- <%! int x = 0; %> -->
This line will declare x variable.

How to add block comments in HTML? [duplicate]

I have some HTML code on a page that I don't want to erase, but make inactive for the short term. How can I make the browser ignore parts of the page in the same way the // works in some programming languages?
Behold HTML comments:
<!-- comment -->
http://www.w3.org/TR/html401/intro/sgmltut.html#idx-HTML
The proper way to delete code without deleting it, of course, is to use version control, which enables you to resurrect old code from the past. Don't get into the habit of accumulating commented-out code in your pages, it's no fun. :)
HTML Comments:
<!-- <div> This div will not show </div> -->
See Reference
CSS Comments:
/* This style will not apply */
See Reference
JavaScript Comments:
// This single line JavaScript code will not execute
OR
/*
The whole block of JavaScript code will not execute
*/
See Reference
To comment block with nested comments: substitute inner (block) comments from "--" to "++"
<!-- *********************************************************************
* IMPORTANT: To uncomment section
* sub inner comments "++" -> "--" & remove this comment
*********************************************************************
<head>
<title>My document's title</title> <++! My document's title ++>
<link rel=stylesheet href="mydoc.css" type="text/css">
</head>
<body>
<++! My document's important HTML stuff ++>
...
...
...
</body>
*********************************************************************
* IMPORTANT: To uncomment section
* sub inner comments "++" -> "--" & remove this comment
*********************************************************************
-->
Thus, the outer most comment ignores all "invalid" inner (block) comments.
Just create a multi-line comment around it. When you want it back, just erase the comment tags.
For example, <!-- Stuff to comment out or make inactive -->
Use:
<!-- This is a comment for an HTML page and it will not display in the browser -->
For more information, I think 3 On SGML and HTML may help you.
If you are using Eclipse then the keyboard shortcut is Ctrl + Shift + / to add a group of code. To make a comment line or select the code, right click -> Source -> Add Block Comment.
To remove the block comment, Ctrl + Shift + \ or right click -> Source -> Remove Block comment.
Reason of comments:
Comment out elements temporarily rather than removing them, especially if they've been left unfinished.
Write notes or reminders to yourself inside your actual HTML documents.
Create notes for other scripting languages like JavaScript which requires them
HTML Comments
<!-- Everything is invisible -->

Are the tags/comments in HTML auto-corrected by browsers?

Instead of
<!--
,
I used
<!-
...and it is working.
How?
It's not actually working - it's just interpreting it as an actual tag, and then throwing that tag out as invalid.
<!- foo bar -->
is treated as a tag, <!-foo bar--> which obviously isn't a standard HTML tag, and thus is ignored.
Try this, and you'll see it's not truly working as a comment:
<!- >foo bar-->
Modern browser parsers (i.e. those that use the HTML5 parsing algorithm) work like this. If they are expecting text or a new tag next, and they see <! then they check the next few characters to see if they are -- or DOCTYPE or, if they are processing embedded SVG or MathML, [CDATA[. (See http://dev.w3.org/html5/spec/tokenization.html#markup-declaration-open-state)
If, as in the case of <!- foo, none of these match then the parser enters the bogus comment state where all the characters following, up to the next >, are read and and converted into a comment to be put into the DOM.
Hence the behaviour you see with <!- working like a comment start. Note that such behaviour is "repair" behaviour for broken markup and it's wise not to rely on it.
You can see how such markup forms a DOM here: Live DOM Viewer
Also note that this is different to what #Amber says. It is not treated as a tag in any meaningful sense, and it is certainly not ignored.

double hyphen in script makes firefox render strangely

<!-- <script type="text/javascript">/*<![CDATA[*/ c-- ;//]]></script> -->
When I have the above line in the <head> section of a plain html page, Firefox 3.5.5 renders the trailing --> as text. If I change c-- to c- it doesn't. Any ideas what's going on here? I getting an artifact on my pages with this due to a very large script that's been crunched. I can change the statement to c-=1 and avoid the problem for now but.... I'd like to know what bit/byte is biting my a$$.
This is due to Firefox implementing SGML (on which HTML was based) comments strictly. This will only occur when the document is loaded in standards mode (i.e. there is a DOCTYPE).
The first <! starts a comment. The first -- enters a section in which > characters are allowed. The second -- (in your script) leaves the section in which > characters are allowed. The > at the end of </script> then ends the comment. The following --> is therefore no longer part of the the comment and gets rendered as text.
See http://www.howtocreate.co.uk/SGMLComments.html for a comprehensive guide to the issue.
Its also worth noting that the HTML 4 Specification says that 'authors should avoid putting two or more adjacent hyphens inside comments' and the HTML 5 Specification says comments must not 'contain two consecutive U+002D HYPHEN-MINUS characters (--)'.
The solution, as you've found, is to not include -- in the middle of a comment.
Technically you are not allowed to have double hyphen in a comment in HTML (or XML). So even if browsers "allow" if it is not valid and should fail an HTML validator.
See Comment section of HTML 4 Specification
I can't replicate this. Doesn't show up on 3.0.1.