When is a CDATA section necessary within a script tag?

后端 未结 15 2269
时光说笑
时光说笑 2020-11-21 22:37

Are CDATA tags ever necessary in script tags and if so when?

In other words, when and where is this:



        
相关标签:
15条回答
  • 2020-11-21 23:17

    CDATA indicates that the contents within are not XML.

    0 讨论(0)
  • 2020-11-21 23:18

    That way older browser don't parse the Javascript code and the page doesn't break.

    Backwards compatability. Gotta love it.

    0 讨论(0)
  • 2020-11-21 23:19

    When browsers treat the markup as XML:

    <script>
    <![CDATA[
        ...code...
    ]]>
    </script>
    

    When browsers treat the markup as HTML:

    <script>
        ...code...
    </script>
    

    When browsers treat the markup as HTML and you want your XHTML 1.0 markup (for example) to validate.

    <script>
    //<![CDATA[
        ...code...
    //]]>
    </script>
    
    0 讨论(0)
提交回复
热议问题