SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question New Line using document.write() and document.writeln()

    I am trying to write a bunch of text onto a new document using document.write() and somehow need to format it to include line breaks.

    For example:

    Code JavaScript:
    document.write(Line 1);
    document.write(Line 2);

    I have tried including \n, and it does not work. I have also tried document.writeln() and that also does not work.

    From what I have found on the Internet, one (if not both) of those methods should have worked..

  2. #2
    SitePoint Enthusiast TriLLi's Avatar
    Join Date
    Feb 2010
    Location
    BiH, Bugojno
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to format document with line breaks than you need to put that text into element pre
    example

    Code HTML4Strict:
    <pre>
     hello,
     
    how are you
     
    are you ok
    </pre>

    sow when you are using document.write do it with this logic
    Code HTML4Strict:
    document.write('<pre>');
    document.write('hello\r\n from here \r\n');
    document.write('</pre>');
    http://www.wiseblog.info
    Programmers don't die, they just GO SUB without return.

  3. #3
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thanks, using document.write('<pre>') worked.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The alternative is to use a proper HTML line break - <br>
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

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