Do browsers widely support numeric quotes in attributes?

笑着哭i 提交于 2020-01-14 06:05:27

问题


There exist other ways of linking to JS, apart from this (the usual)..

<script src="myscript.js" type="text/javascript"></script>

...that utilize other quote types:

<script src=&#34;myscript.js&#34; type=&#34;text/javascript&#34;></script>

Are these widely supported in modern browsers, and older browsers in use, such as IE6? Basically is it safe to use this method, just as you would use the regular double-quote method?


Edit: The HTML4 spec seems to allow it, but is it well supported in practical reality?

3.2.2 Attributes

  Authors may also use numeric character references to represent
  double quotes (&#34;) and single quotes (&#39;).

  For double quotes authors can also use the
  character entity reference &quot;.

回答1:


Using &#34; instead of " is simply wrong, it doesn't have the same meaning within the SGML and XML specifications. Argument values of elements should use either single (') or double quotes ("). In the old SGML specification this element

<foo bar=&#34;quux&#34; />

could be read as an element with the name foo, and attribute named bar with the value "quux". However, the standard defines that unquoted attribute values should not include escaped characters. And this element

<foo bar="quux" />

should be read as an element with the name foo, and attribute named bar with the value quux without the quotes. This is because in SGML the quotes are optional, and everything up to the next space will be used as the value for the attribute.

XML requires quotes.




回答2:


There is a difference between an attribute value delimiter and a quote or double quote character.

You have to use a literal " or ' to delimit attribute values (except where delimiters are optional). In this case, the squence of bytes means "attribute value delimited" not "(double) quote mark"

The character references can be used to represent a (double) quote mark but is a more complicated and inefficient way compared to using a literal so should only be used when the literal is not available (i.e. when it would be an attribute value delimiter because you are inside an an attribute value where the opening delimiter was that character).




回答3:


Just out of curiosity. Why would you want to use the encoded variants? In most of the text editors it will break the formatting. For me that would be very annoying.




回答4:


You should stick with the double quotes - othewise the attribute might not be correctly read.




回答5:


<script src=myscript.js></script>

is valid in HTML5 and supported by every significant browser.



来源:https://stackoverflow.com/questions/963679/do-browsers-widely-support-numeric-quotes-in-attributes

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