Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

When is a CDATA section necessary within a script tag?

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

In other words, when and where is this:

<script type="text/javascript">
//<![CDATA[
...code...
//]]>
</script>

preferable to this:

<script type="text/javascript">
...code...
</script>

Answer*

Draft saved
Draft discarded
Cancel
7
  • 3
    The statement "A CDATA section has no effect at all" is not true for (the proposed) HTML5, which recognizes the construct. w3.org/TR/html5/syntax.html#cdata-sections Commented Mar 6, 2012 at 20:29
  • 3
    @danorton Interesting. I think that's a pretty ugly mix. Still no effect in script content though. Commented Mar 6, 2012 at 22:06
  • 2
    Did not know that any </ inside script tags is bad. Commented Mar 5, 2013 at 18:38
  • 3
    @SalmanA That's one of HTML's odditys and officially called ETAGO. Learn more: mathiasbynens.be/notes/etago (while the article states that no browser ever implemented that feature, I'm pretty sure it caused some trouble for me. Maybe in some other tool) Commented Mar 5, 2013 at 20:35
  • 1
    Actually I ran into validation problems -- <script>var b = "<b>bold</b>";</script> fails to validate but after reading your answer and changing to <script>var b = "<b>bold<\/b>";</script> fixed it. Commented Mar 5, 2013 at 21:52

default

AltStyle によって変換されたページ (->オリジナル) /