write NLCR to txt output

Discussion in 'Javascript' started by Tom de Neef, Feb 27, 2008.

  1. Tom de Neef

    Tom de Neef Guest

    I try to construct a string for output in the body of an email. My problem
    is to get new line/carriage return characters inserted.

    On reading a txt file I can split lines successfully with split(/[\r\n]/)
    (thanks to Thomas Lahn and Evertjan) or
    split(String.fromCharCode(13)+String.fromCharCode(10)) (which is the
    traditional #13#10 found in txt files).
    But I fail in the reverse process. Neither

    var s = "this \r\nor that"
    nor
    var s = "this '+String.fromCharCode(13)+String.fromCharCode(10)+'or that'

    produces a new line during output to a mail body in
    location="mailto:?subject=This or that&body="+s
    (Both work fine in alert(s) )

    So: how to force a new line in txt output ?
    Thanks
    Tom
    Tom de Neef, Feb 27, 2008
    #1
    1. Advertising

  2. Tom de Neef

    GArlington Guest

    On Feb 27, 8:07 am, "Tom de Neef" <> wrote:
    > I try to construct a string for output in the body of an email. My problem
    > is to get new line/carriage return characters inserted.
    >
    > On reading a txt file I can split lines successfully with split(/[\r\n]/)
    > (thanks to Thomas Lahn and Evertjan) or
    > split(String.fromCharCode(13)+String.fromCharCode(10)) (which is the
    > traditional #13#10 found in txt files).
    > But I fail in the reverse process. Neither
    >
    > var s = "this \r\nor that"
    > nor
    > var s = "this '+String.fromCharCode(13)+String.fromCharCode(10)+'or that'
    >
    > produces a new line during output to a mail body in
    > location="mailto:?subject=This or that&body="+s
    > (Both work fine in alert(s) )
    >
    > So: how to force a new line in txt output ?
    > Thanks
    > Tom


    It took me few attempts to remember how I used to do that, try this:
    <script type="text/javascript">
    var mailBody = "blah" + '\r\n' + "blah" + '\r\n' + "blah";
    var newLocation = "mailto:?subject=This subject&body="
    + escape(mailBody);
    alert(newLocation);
    location = newLocation;
    </script>
    GArlington, Feb 27, 2008
    #2
    1. Advertising

  3. Tom de Neef

    Tom de Neef Guest

    "GArlington" wrote in respons to:
    >> I try to construct a string for output in the body of an email. My
    >> problem
    >> is to get new line/carriage return characters inserted.
    >>
    >> On reading a txt file I can split lines successfully with split(/[\r\n]/)
    >> (thanks to Thomas Lahn and Evertjan) or
    >> split(String.fromCharCode(13)+String.fromCharCode(10)) (which is the
    >> traditional #13#10 found in txt files).
    >> But I fail in the reverse process. Neither
    >>
    >> var s = "this \r\nor that"
    >> nor
    >> var s = "this '+String.fromCharCode(13)+String.fromCharCode(10)+'or that'
    >>
    >> produces a new line during output to a mail body in
    >> location="mailto:?subject=This or that&body="+s
    >> (Both work fine in alert(s) )
    >>
    >> So: how to force a new line in txt output ?
    >> Thanks
    >> Tom

    >
    > It took me few attempts to remember how I used to do that, try this:
    > <script type="text/javascript">
    > var mailBody = "blah" + '\r\n' + "blah" + '\r\n' + "blah";
    > var newLocation = "mailto:?subject=This subject&body="
    > + escape(mailBody);
    > alert(newLocation);
    > location = newLocation;
    > </script>


    Ah, escape(), that does it. Thank you.
    Tom
    Tom de Neef, Feb 27, 2008
    #3
  4. Tom de Neef wrote:
    > "GArlington" wrote in respons to:
    >> It took me few attempts to remember how I used to do that, try this:
    >> <script type="text/javascript">
    >> var mailBody = "blah" + '\r\n' + "blah" + '\r\n' + "blah";
    >> var newLocation = "mailto:?subject=This subject&body="
    >> + escape(mailBody);
    >> alert(newLocation);
    >> location = newLocation;
    >> </script>

    >
    > Ah, escape(), that does it. Thank you.


    Nevertheless, it is likely that the `mailto:' URI is going to fail
    miserably, given the apparently predominant amount of Web mail accounts,
    and the existence of Internet caf├ęs. You are much better off using a
    server-side formmailer instead noawadys, also regarding e-mail spam.

    BTW, it's called CRLF (Carriage Return, Line Feed: \r [0x0D] followed by \n
    [0x0A]).


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
    Thomas 'PointedEars' Lahn, Feb 28, 2008
    #4
    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. palmis

    write into file.txt

    palmis, Jan 10, 2006, in forum: Java
    Replies:
    5
    Views:
    513
    Roedy Green
    Jan 10, 2006
  2. Fredrik Lundh
    Replies:
    0
    Views:
    565
    Fredrik Lundh
    Nov 18, 2005
  3. Pitmairen
    Replies:
    4
    Views:
    517
    Bruno Desthuilliers
    Mar 6, 2006
  4. Sameen
    Replies:
    2
    Views:
    415
    Victor Bazarov
    Aug 29, 2005
  5. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,445
    Roedy Green
    Sep 15, 2011
Loading...

Share This Page