The reason why I can't insert a comment inside the HTML tag?

Is there any reason why I am unable to insert a comment inside the HTML tag?

Example: in HTML (not possible)

 <img <!-- sample comment--> src="" alt="Sample Picture" class="img-circle center-block" />

      

It's easy to do this in JavaScript so far.

Example: in JS (maybe)

verticalCentered: /* Sample Comment*/ true

      

Please explain this in detail.

+3


source to share


4 answers


HTML was originally defined formally as an SGML application, and the comment syntax was taken from SGML. SGML uses two hyphens --

as comment separators, but only allows comments in certain contexts. Of these contexts, there is only a comment declaration in HTML. (Well, in theory they are also allowed in entity declarations such as <!ENTITY foo "foo" -- Comment -- >

, but entity declarations were never implemented in any browser prior to XHTML, and in XHTML, following XML syntax, entity declarations cannot contain comments.)

Thus, comments are only allowed in comment ads that contain only comments, for example.

<!-- Comment one -- -- Comment two -->

      

Browsers have actually implemented this in part in a simplified (incorrect) way, so a rule of thumb is to only have comments in comment declarations containing one comment:



<!-- Comment -->

      

Comment declarations are at the same structural level as tags and therefore cannot appear in tags. (Comments cannot contain tags: anything that would otherwise constitute a tag, for example, <p>

is treated as character data within a comment.)

This has not changed in later versions of HTML. They have simplified the syntax in matters like this rather than extended.

+5


source


If you use a template language, you can use the mechanism of comments template language to do what you want: <img {{! this is a comment }} src=...

. This also has the advantage that the comment will be removed at compile time and thus will not interfere with HTML comments.



+4


source


Since comment is also a tag and you cannot write html tag inside html anther tag.

-2


source


You can insert a comment in the html tag. If im using netbeans then you can turn off HTML error checking when an error appears

-6


source







All Articles