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 8308617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:51:49+00:00 2026-06-08T18:51:49+00:00

I want to initialize a Javascript variable from some JSON (generated via Jackson) in

  • 0

I want to initialize a Javascript variable from some JSON (generated via Jackson) in my JSPX, something like this:

<script>
var x = <c:out value="${myJson}" />;
</script>

But the output I get looks like:

<script>
var x = {&#034;foo&#034;:&#034bar&#034};
</script>

I see what you did there, HTML-escaping the string. Obviously, I can’t leave it completely unescaped because angle brackets in the data could break the page. But I don’t really need all the quotes to be escaped, since I’m not putting the JSON within an attribute value, do I?

Now, this looks like it would be a perfectly valid way to write a script in HTML, just needlessly complicated (like, say, replacing spaces with &#32;). As it turns out, it works just fine in XHTML, but with an HTML content type, I get an error, both in Firefox and IE. I’m not sure of the rationale, but that’s how it is.

So, what’s the best approach here? Do I really want to simply escape angle brackets but not escape double quotes, or are there any other gotchas? Is there a tag out there that would replace c:out (I know there are Spring tags for escaping Javascript, but that’s still not the right kind of escaping)? How do people get this to work?

BTW, yes, I could make a separate AJAX call, but an extra round trip just to work around this problem seems silly.

UPDATE

I had a lot to learn about CDATA vs. PCDATA and how HTML is different from XHTML. Here I thought JSPX would make polyglot markup easy, but it turns out to be, as someone put it, a big ball of nasty.

For HTML, the <script> element has a CDATA content model (not to be confused with CDATA sections), which means nothing can be escaped, but </ must be absolutely avoided.

In the special case of JSON, where end tags can only occur within a quoted string, this therefore means the safe way to escape is to use Javascript (rather than HTML) escaping and replace </ with <\/.

For XHTML (if you care about such things) on the other hand, you just XML-escape everything as usual (& becomes &amp;, etc.) and it all works beautifully. A compatible solution would have to use CDATA with guarding comments (<!--/*--><![CDATA[/*><!--*/ etc.) around the entire <script> body and then escape any occurrences of ]]> within the JSON; furthermore, I’d still escape </ too just to be safe. Big ball of nasty, indeed, but at least it can be automated.

  • 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-06-08T18:51:50+00:00Added an answer on June 8, 2026 at 6:51 pm

    OK, answering my own question here, after much research and no real help.

    Based my “update” above, the most straightforward way targeting HTML is just:

    <script>
    var x = ${fn:replace(myJson, "\</", "\<\\/")};
    </script>
    

    Ugly but simple.

    This will not yield valid XML or XHTML, unfortunately. If you really need that, the original c:out will work fine, though it will not yield valid HTML. And if you really need a single solution to work on both, you probably need a custom taglib (or TAGX) that will either switch from the content type or do all of the following:

    • wrap the script body in a comment-guarded CDATA section
    • replace each </ with <\/
    • replace each ]]> with ]]\>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to run a JavaScript function to initialize some controls. My problem is
I want to initialize JavaScript array value from c# in asp.net application. I have
I want to initialize a static Class variable in Java: public class NumberExpression {
I have some javaScript Classes ( ctor+prototype methods) that I want their instances to
I am new to Javascript. I want to fetch lat-long from MySQL (more then
I have google map on my site(php,apache,mysql). here is code: <script type=text/javascript> function initialize()
I have a JS sort script, but is ordering #'s like this: 1. 10.
I'm having a stupid hard time with this google maps javascript code. I want
I want to initialize a couple of variables on Excel Dna .dll gets loaded
I want to initialize an array and then initialize a pointer to that array.

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.