1

I would like to know how can I insert a string into a javascript code like in the example below:

     <script type="application/ld+json">
        {
          "@context": "http://schema.org",
          "@type": "Restaurant",
          "name": "Dave's Steak House",
          "address": {
            "@type": "PostalAddress",
            "streetAddress": "148 W 51st St",
            "addressLocality": "New York",
            "addressRegion": "NY",
            "postalCode": "10019",
            "addressCountry": "US"
          }
        }
        </script>

The problem is that when I try to insert the String between js it appears interpreted as HTML code with %, &20 etc. I have tried with ?html, escape, no escape, ?string, ?js_string, etc.

That's the String:

 {
          "@context": "http://schema.org",
          "@type": "Restaurant",
          "name": "Dave's Steak House",
          "address": {
            "@type": "PostalAddress",
            "streetAddress": "148 W 51st St",
            "addressLocality": "New York",
            "addressRegion": "NY",
            "postalCode": "10019",
            "addressCountry": "US"
          }
        }

And the code:

${myString}
3
  • Possible duplicate of How to insert a large block of HTML in JavaScript? Commented Jul 11, 2016 at 10:54
  • You do it like ${myString} usually... can you explain better what happens then? What do you mean it's interpreted as HTML code? What's % and &20? Commented Jul 11, 2016 at 17:21
  • For example whitespaces are interpreted as &nbsp; I mean that all is interpreted as ASCII code. Commented Jul 12, 2016 at 6:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.