www.webdeveloper.com
Results 1 to 6 of 6

Thread: \n new line character won't work

Hybrid View

  1. #1
    Join Date
    Dec 2012
    Posts
    2

    \n new line character won't work

    Hello,
    I am studying javascript from eloquent javascript book.
    It says:
    "This is the first line\nAnd this is the second"
    I tried making a web page by writing
    document.write("This is the first line\nAnd this is the second");

    The desired effect is:

    This is the first line
    And this is the second

    But I get this:

    This is the first line And this is the second

    I tried writing <br> inside and it worked. But Why writing an escape \n didn't? Anything I am doing wrong? I am using firefox

    Thank you

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,436
    You are doing nothing wrong. The \n character is valid in text related elements like, alert() and <textarea>.

    The document.write() command writes to the body of the site or the display. Substitute <br> in place of the \n here.

  3. #3
    Join Date
    Dec 2012
    Posts
    2
    Ah thank you, the book did not clarify that.

  4. #4
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    To add to what JMRKER said, whitespace is handled automatically if it's in the body of your page, but not when used in alert boxes and input elements etc. So all your whitespace characters (spaces, new lines etc.) are treated like a single space until the text needs to wrap to the next line, in which case the whitespace character (whatever it is) is treated like a newline character.

    Another example that would show this would be writing several spaces after each other:
    Code:
    alert("Hello foo    bar."); // shows several spaces between foo and bar
    document.write("Hello foo    bar."); // shows only a single space

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,436
    Note also that <p> can be used to replace <br><br>
    and &nbsp; can be use in the body to force extra spaces to be visible,
    as in &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; to show 5 blank spaces.

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; could be used to display 9 spaces between visible text characters

  6. #6
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    Quote Originally Posted by JMRKER View Post
    &nbsp; can be use in the body to force extra spaces to be visible,
    as in &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; to show 5 blank spaces.
    Although, the CSS properties 'margin' and 'padding' should be used to add extra space around and inside an element if possible.

    (It feels like we're slowly drifting farther and farther away from the topic by every post. lol)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles