How to write Multiple Lines in JS?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wow

    #1

    How to write Multiple Lines in JS?

    Do I have to replace all new lines with \n and document.write one line? Is
    there something like perl's print lines

    print << 'body';



    body

    in javascripts?


  • Grant Wagner

    #2
    Re: How to write Multiple Lines in JS?

    Wow wrote:
    [color=blue]
    > Do I have to replace all new lines with \n and document.write one line? Is
    > there something like perl's print lines
    >
    > print << 'body';
    >
    > body
    >
    > in javascripts?[/color]

    No, JavaScript has no concept like the here document in Perl. The best you can
    do is use something like the following if you have lots of stuff to
    document.write( ):

    document.write(
    'stuff' +
    ' more stuff' +
    ' even more stuff' +
    );

    Since including \n in the output of a document.write( ) into the <body> of an
    HTML document simply results in a space in the output, you might as well use
    spaces and save yourself some typing. Another way to do this would be:

    var output = [
    'stuff',
    'more stuff',
    'even more stuff'
    ];
    document.write( output.join('\n '));

    --
    Grant Wagner <gwagner@agrico reunited.com>
    comp.lang.javas cript FAQ - http://jibbering.com/faq

    Comment

    • kaeli

      #3
      Re: How to write Multiple Lines in JS?

      In article <HPZMc.1221913$ Ar.1037509
      @twister01.bloo r.is.net.cable. rogers.com>, a@alexa.com enlightened us with...[color=blue]
      > Do I have to replace all new lines with \n and document.write one line? Is
      > there something like perl's print lines
      >
      > print << 'body';
      >
      >
      >[/color]

      IF I understand your question correctly...

      var myString;
      myString = "This is a line of text " +
      "that I want over multiple lines. " +
      "That way, I don't have to make one big line of "+
      "unreadable text.";

      document.write( myString);

      The string will be all on one line in the browser.

      As to a construct like the HERE doc in Perl, no, no such animal in JS AFAIK.

      --
      --
      ~kaeli~
      When you choke a smurf, what color does it turn?



      Comment

      Working...