-2

I want to build a long string in JavaScript that is made up of many components and I'd like use multiple lines to make the code easier to understand. I tried:

data = '<!doctype html>
    <!-- HTML5  -->
    <html>
    <head>
    <meta charset="utf-8" />
    <title>Webplaces</title>' 
    + 
    styleBlock 
        +
    '</head> 
    <body>' 
        +
    outerHTML  
        + 
    '</body>
    </html>' ;  

but JavaScript doesn't like that at all. Is there a way to do this?

Thanks

Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123
Steve
  • 4,534
  • 9
  • 52
  • 110

1 Answers1

0

You have to make each new line its own string and concatenate them:

data = "<!doctype html>"+
    "<!-- HTML5  -->"+
    "<html>"+
    "<head>"+
    "<meta charset="utf-8" />"+
    "<title>Webplaces</title>"+
    styleBlock+
    "</head>"+ 
    "<body>"+
    outerHTML+
    "</body>"+
    "</html>";  

It is possible to escape the lines with a slash \ like this:

text = "This line will \
    end here";

But that isn't recommended because the whitespace (indent) that appears at the beginning of each line can possibly lead to errors.

Enigmadan
  • 3,398
  • 2
  • 23
  • 35