Insert a new line character into a string literal

Discussion in 'Javascript' started by Water Cooler v2, Apr 18, 2006.

  1. Besides embedding <BR/>, like general purpose programming languages, I
    thought you could embed escape sequences such as \n or \r\n into string
    arguments to cause line breaks in JavaScript.


    To test, I tried this:


    <BODY>
    <BR/><BR/>
    <SCRIPT Language="JavaScript" type="text/javascript">
    <!--
    document.write("<B>Hello, World!</B>"+'\r\n'+"OK! In my next example,
    I am going to put one JavaScript function in...")

    document.write("<B>Hello, World!</B>"+"\r\nOK! In my next example, I am
    going to put one JavaScript function in...")


    document.write("<B>Hello, World!</B>"+"\n"+"OK! In my next example, I
    am going to put one JavaScript function in...")

    document.write("<B>Hello, World!</B>"+'"r\n"+"OK! In my next example, I
    am going to put one JavaScript function in...")


    //-->
    </SCRIPT>
    </BODY>


    None of it inserts a line break on the display text.

    A second question I have is, when does one use single quote delimited
    strings and when double-quote delimited strings? Is it like C, wherein
    single character values must be emebedded in single-quotes whereas
    character array literals (or strings, if you will) must be packaged in
    double-quoted strings? Or is it loosely defined?
    Water Cooler v2, Apr 18, 2006
    #1
    1. Advertising

  2. On 18/04/2006 10:03, Water Cooler v2 wrote:

    > Besides embedding <BR/>,


    You clearly aren't writing XHTML, so lose the XML empty-element syntax.

    > like general purpose programming languages, I thought you could embed
    > escape sequences such as \n or \r\n into string arguments to cause
    > line breaks in JavaScript.


    Yes, one can.

    [snip]

    > <SCRIPT Language="JavaScript" type="text/javascript">


    The language attribute has long been deprecated. Omit it.

    > <!--


    'Hiding' script content has not been necessary for years. Omit markup
    comments, too.

    > document.write("<B>Hello, World!</B>"+'\r\n'+"OK! In my next example,
    > I am going to put one JavaScript function in...")


    [snip]

    > None of it inserts a line break on the display text.


    Of course not, for precisely the same reason that

    <p>Some text split
    over multiple lines.</p>

    will render as one line (viewport width permitting): line terminators
    are considered to be plain whitespace, just like tabs and spaces.

    You should consider reading section "9.1 White space" in the HTML
    specification.

    When strings containing line terminators are written into a HTML
    document, the line breaks will exist within the generated source.

    > A second question I have is, when does one use single quote delimited
    > strings and when double-quote delimited strings?


    When it's more convenient to use one over the other.

    > Is it like C, wherein single character values must be emebedded in
    > single-quotes whereas character array literals (or strings, if you
    > will) must be packaged in double-quoted strings? Or is it loosely
    > defined?


    The latter. One can pick and choose to best fit the circumstances.
    Personally, I tend to use single quotes.

    Mike


    Please don't use tabs to indent code. Use a number of spaces, preferably
    two, and four at most. Wrap lines manually to ensure that automatic
    wrapping doesn't create syntax errors.

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
    Michael Winter, Apr 18, 2006
    #2
    1. Advertising

  3. Thanks a tonne, Mike, for that invaluable advise. I am not sure I'd
    have got those tips out of any tutorial.


    >Of course not, for precisely the same reason that...


    Thanks. I tried what you said and it worked. I did this:

    <HTML>
    <BODY>
    <SCRIPT>
    document.write("<PRE>foo\nbar</PRE>");
    </SCRIPT>
    </BODY>
    </HTML>

    and lo and behold! I got a line break interspercing the two words.


    However, I could not completely understand the truth in this:

    >When strings containing line terminators are written into a HTML
    >document, the line breaks will exist within the generated source.



    So, to try it, I removed the PRE tag in the above example and simply
    made it:

    document.write("foo\nbar");

    and as expected, I got "foo bar" displayed on the browser, but the
    source of the page was, just as I'd expected, the same as
    document.write("foo\nbar"). I am sure I am misinterpreting that
    statement of yours in someway. It'd be wiered if the client/browser
    messed up with JavaScript and converted it to HTML source. What am I
    missing?
    Water Cooler v2, Apr 18, 2006
    #3
  4. Water Cooler v2

    RobG Guest

    Water Cooler v2 wrote:
    > Thanks a tonne, Mike, for that invaluable advise. I am not sure I'd
    > have got those tips out of any tutorial.
    >
    >
    >> Of course not, for precisely the same reason that...

    >
    > Thanks. I tried what you said and it worked. I did this:
    >
    > <HTML>
    > <BODY>
    > <SCRIPT>
    > document.write("<PRE>foo\nbar</PRE>");
    > </SCRIPT>
    > </BODY>
    > </HTML>
    >
    > and lo and behold! I got a line break interspercing the two words.
    >
    >
    > However, I could not completely understand the truth in this:
    >
    >> When strings containing line terminators are written into a HTML
    >> document, the line breaks will exist within the generated source.

    >
    >
    > So, to try it, I removed the PRE tag in the above example and simply
    > made it:
    >
    > document.write("foo\nbar");
    >
    > and as expected, I got "foo bar" displayed on the browser, but the
    > source of the page was, just as I'd expected, the same as
    > document.write("foo\nbar").


    Because that *is* the page source. What is displayed in the browser is
    the generated code from the document.write statement. If you want to
    see what the generated code looks like, create a bookmark with this as
    the URL (remove newlines, paste as one single line with no spaces):

    javascript:document.write('<code><ol><li>'+(document.documentElement||document.body).innerHTML.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/%20%20/g,"&nbsp;%20").replace(/\n/g,"<li>")+'<\/ol><\/code>');

    Now you will see (with line numbers):

    <script type="text/javascript">
    document.write("foo\nbar");
    </script>foo
    bar


    --
    Rob
    RobG, Apr 18, 2006
    #4
  5. On 18/04/2006 12:44, RobG wrote:

    [snip]

    > If you want to see what the generated code looks like, [...]


    ....download a recent version of Chris Pederick's Web Developer extension
    for Firefox. :)

    [snip]

    Mike

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
    Michael Winter, Apr 18, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guadala Harry
    Replies:
    14
    Views:
    1,377
    Guadala Harry
    Apr 27, 2005
  2. Andreas Leitgeb
    Replies:
    0
    Views:
    448
    Andreas Leitgeb
    May 15, 2009
  3. Mark Space
    Replies:
    0
    Views:
    481
    Mark Space
    May 15, 2009
  4. Lew
    Replies:
    0
    Views:
    943
  5. Joshua Cranmer
    Replies:
    0
    Views:
    459
    Joshua Cranmer
    May 15, 2009
Loading...

Share This Page