Should I use <![CDATA[…]]> in HTML5?

前端 未结 4 688
傲寒
傲寒 2020-12-12 19:17

I\'m pretty sure sections can be used in XHTML5, but what about HTML5?

4条回答
  •  鱼传尺愫
    2020-12-12 19:58

    From the same page @pst linked to:

    Element-specific parsing for script and style tags, Guidance for XHTML-HTML compatibility: "The following code with escaping can ensure script and style elements will work in both XHTML and HTML, including older browsers."

    Maximum backwards compatibility:

    
    

    Simpler version, sort of incompatible with "much older browsers":

    
    

    So, CDATA can be used in HTML5, and it's recommended in the Guidance for XHTML-HTML compatibility. This useful for polyglot HTML/XML/XHTML pages, which are created and parsed as XML during development, but served as HTML5 for better cross-browser compatibility. Polyglot pages has their benefits, and I've used this myself, as it's much easier to debug XML/XHTML5. Google Chrome, for example, will throw an error for invalid XML/XHTML5 (including for example character escaping), whereas the same page served as HTML5 will "just work" aka "probably work".

提交回复
热议问题