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

  • Home
  • SEARCH
  • 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 3352076
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:59:10+00:00 2026-05-18T01:59:10+00:00

The only way I know how to print a huge string without using +=

  • 0

The only way I know how to print a huge string without using += is to use \ backslashes. ugly!

<div id="foo"></div>
<script type="text/javascript">
var longString = '<div id="lol">\
        <div id="otherstuff">\
            test content. maybe some code\
        </div>\
    </div>';

document.getElementById('foo').innerHTML = longString;
</script>

is there any way to do this where the longString is untainted? php has $foo = ”’ long multiline string ”’; I want this in javascript!

Anyone know of a better method for printing long, multi-line strings in javascript?

  • 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-18T01:59:10+00:00Added an answer on May 18, 2026 at 1:59 am

    In general, the answer is: not in the language syntax. Though as Ken pointed out in his answer there are many work-arounds (my personal method is to load a file via AJAX). In your specific case though, I’d prefer creating a HTML constructor function so you can then define the HTML structure using javascript object literals. Something like:

    var longString = makeHTML([{
      div : {
        id : "lol",
        children : [{
          div : {
            id : "otherstuff",
            children : [{
                text : "test content. maybe some code"
            }]
        }]
     }]
    

    which I find to be much easier to handle. Plus, you this would allow you to use real function literals when you need it to avoid string quoting hell:

    makeHTML([{
      span : {
        onclick : function (event) {/* do something */}
      }
    }]);
    

    note: the implementation of makeHTML is left as exercise for the reader


    Additional answer:

    Found some old code after a quick scan through my hard disk. It’s a bit different from what I suggested above so I thought I’d share it to illustrate one of the many ways you can write functions like this. Javascript is a very flexible language and there is not much that forces you to write code one way or another. Choose the API you feel most natural and comfortable and write code to implement it.

    Here’s the code:

    function makeElement (tag, spec, children) {
        var el = document.createElement(tag);
        for (var n in spec) {
            if (n == 'style') {
                setStyle(el,spec[n]);
            }
            else {
                el[n] = spec[n];
            }
        }
        if (children && children.length) {
            for (var i=0; i<children.length; i++) {
                el.appendChild(children[i]);
            }
        }
        return el;
    }
    
    /* implementation of setStyle is
     * left as exercise for the reader
     */
    

    Using it would be something like:

    document.getElementById('foo').appendChild(
      makeElement(div,{id:"lol"},[
        makeElement(div,{id:"otherstuff"},[
          makeText("test content. maybe some code")
        ])
      ])
    );
    
    /* implementation of makeText is
     * left as exercise for the reader
     */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how can I search a list of tuples if I only know 1 element
Is there simply way to dinamic generate untrusted ssl certificate without domain and applay
I have a C++ dll reporting lots of useful information via console output using
Here I am trying to filter only the elements that do not have a
Is there an easy way to get a python code segment to run every
As we know in C++ we have class iostream, which is inherited from istream(basic_istream)
I am trying to make my game a bit easier on the phone, so
All: a = 1 b = a c = b Now I want to
I need to extract requests from a log file that look like this :

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.