Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 659757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:04:49+00:00 2026-05-13T23:04:49+00:00

I want to inline scripts or CSSs into XHTML without escaping special characters. I

  • 0

I want to inline scripts or CSSs into XHTML without escaping special characters.

I can do that using a CDATA marked section.

According to http://www.w3.org/TR/xhtml1/#h-4.8 the CDATA section can be defined as:

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

Then, according to http://www.w3schools.com/TAGS/tag_script.asp, the CDATA can look like:

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

Which method for closing the CDATA section is better? ]]> or //]]> ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:04:50+00:00Added an answer on May 13, 2026 at 11:04 pm

    According to http://www.w3.org/TR/xhtml1/#h-4.8 the CDATA section can be defined as: [no //]

    Yeah. In XHTML, they can. Proper XHTML, as read by an XML parser like when you serve application/xhtml+xml to a web browser that isn’t IE.

    But probably you’re actually serving as text/html, which means your browser isn’t an ‘XML processor’ as referenced in that section. It’s a legacy-HTML4 parser, so you have to abide by the appendix C guidelines and avoid any XML features that don’t work in HTML4.

    In particular, the strings <![CDATA[ and ]]> in a <script> or <style> block are not special to an HTML4 parser, because in HTML4 those two elements are ‘CDATA elements’ where markup doesn’t apply (except for the </ ETAGO sequence to end the element itself). So an HTML4 parser will send those strings straight to the CSS or JavaScript engine.

    Because <![CDATA[ is not valid JS, you’ll get a JavaScript syntax error. (The other answers are wrong here: it’s not just very old browsers, but all HTML4 browsers, that will give errors for an uncommented CDATA section in script.)

    You use the // or /* comment markup to hide the content from the JavaScript or CSS engine. So:

    <script type="text/javascript">//<![CDATA[
        alert('a&b');
    //]]></script>
    

    (Note the leading //; this was omitted in the W3Schools example code, and makes that example code not work at all. Fail. Don’t trust W3Schools: they are nothing to do with W3C and their material is often rubbish.)

    This is read by an HTML parser as:

    • Open-tag script establishing CDATA content until the next ETAGO
    • Text //<![CDATA[\n alert('a&b');\n//]]>
    • ETAGO and close-tag script
    • -> resultant content sent to JavaScript engine: //<![CDATA[\nalert('a&b');\n//]]>

    But by an XML parser as:

    • Open-tag script (no special parsing implications)
    • Text content //
    • Open CDATA section establishing CDATA content until the next ]]> sequence
    • Text \n alert('a&b');\n//
    • Close CDATA section
    • Close-tag script
    • -> resultant content sent to JavaScript engine: //\nalert('a&b');\n//

    Whilst the parsing process is quite different, the JS engine ends up with the same effective code in each case, as thanks to the //​s the only difference is in the comments.

    Note this is a very different case to the old-school:

    <script type="text/javascript"><!--
        alert('a&b');
    //--></script>
    

    which was to hide script/style content so that it didn’t get written onto the page in browsers that didn’t understand <script> and <style> tags. This will not generate a JavaScript/CSS error, because a hack was put it at a different level: it is a syntactical feature of the CSS and JavaScript languages themselves that <!-- is defined to do nothing, allowing this hack to work.

    Those browsers are ancient history; you absolutely should not use this technique today. Especially in XHTML, as an XML parser would take you at your word, turning the whole script block into an XML comment instead of executable code.

    I want to inline Scripts or CSSs into xHTML without escaping special characters.

    Avoid doing this and you will be much happier.

    Do you really need the < and & characters in a <style>? No, almost never. Do you really need them in <script>? Well… sometimes, yeah, and in that case the commented-CDATA-section is acceptable.

    But to be honest, XHTML compatibility guideline C.4 is as applicable to HTML4 as it is to XHTML1: anything non-trivial should be an in external script, and then you don’t have to worry about any of this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 323k
  • Answers 323k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer you need some way to distinguish latest comment, so I… May 14, 2026 at 12:57 am
  • Editorial Team
    Editorial Team added an answer Within Rails' implementation of REST new and create are treated… May 14, 2026 at 12:57 am
  • Editorial Team
    Editorial Team added an answer Up until 5.3 PHP was limited to a single global… May 14, 2026 at 12:57 am

Related Questions

I would like to write simple scripts in C#. Stuff I would normally use
I just realized that I lack the fundamental knowledge of what exactly happens as
This is my first time using Stack Overflow, so if I've done something wrong
I would like to initialize an array of structs, with the same element repetitively,
Using VS2008 and ASP.NET 3.5 (or VS 2010 / .NET 4.0?), how can I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.