double hyphen in script makes firefox render strangely

柔情痞子 提交于 2019-12-20 04:15:12

问题


<!-- <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$$.


回答1:


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.




回答2:


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




回答3:


I can't replicate this. Doesn't show up on 3.0.1.



来源:https://stackoverflow.com/questions/1850439/double-hyphen-in-script-makes-firefox-render-strangely

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!