2

For some reason \n isn't working for me in javascript. Is there anyway to fix it or use html? In my case, I think that using <br> will not work in my href. What are your suggestions.

Working JSFiddle here.

HTML:

<a id = 'emailoff' href = "" target = "_blank">
    <div id= "btn1">
        <h2 id = "enter">Send Email</h2>
    </div>
</a>

Javascript:

$('#btn1').click(function() {
    $("#emailoff").attr("href", "mailto:" +
        "?subject=Your ThinOptics glasses" +
        "&body=To get your new ThinOptics glasses simply click this link and pick the case and color you like best.  You'll get  free shipping on your order. \n"+
        " WWw.Thinoptics.Com/teddy@shalon.com \n" +
        "\n Enjoy")
});
talemyn
  • 7,822
  • 4
  • 31
  • 52
Liam Shalon
  • 422
  • 2
  • 5
  • 19
  • 3
    `\n` does work in JavaScript. But your problem is that the href gets multilined what you properly wants is to escape the new line so the mail client gets `\n` and can treat it as `\n`: TLDR: Use `\\n` but the support can differ from client to client. – Andreas Louv Apr 17 '14 at 19:24
  • 1
    this might answer your question http://stackoverflow.com/questions/1155678/javascript-string-newline-character – one stevy boi Apr 17 '14 at 19:24
  • @NULL "\\n" doesn't seem to be working... do you mean replacing "\n" with "\\n"? – Liam Shalon Apr 17 '14 at 19:26
  • You are not inserting anything dynamically at all in your href, so why not just insert the href attribute when rendering the page? – Mike Brant Apr 17 '14 at 19:28
  • Try %0D instead of \n – Jonathan Anctil Apr 17 '14 at 19:28
  • @epascarello It's true, but the OP wouldn't have known to search for that, they were under the impression that the problem was JS escaping. I would leave it open so that others can find the answer in case they don't understand the problem well. – Ruan Mendes Apr 17 '14 at 19:45

2 Answers2

8

You have to URL encode your new lines. The encoded new line should be %0A http://jsfiddle.net/mendesjuan/LSUAh/

<a id = 'emailoff' href = "mailto:me@you.com?subject=hello&body=a%0Ab" target = "_blank">
    <div id= "btn1">
        <h2 id = "enter">Send Email</h2>
    </div>
</a>

If you're doing it in JS, use the following

$('#btn1').click(function() {
    $("#emailoff").attr("href", "mailto:" +
        "?subject=Your ThinOptics glasses" +
        "&body=To get your new ThinOptics glasses simply click this link and pick the case and color you like best.  You'll get  free shipping on your order. %0A"+
         " WWw.Thinoptics.Com/teddy@shalon.com  %0A" +
        " %0A Enjoy")
});

Note that you don't have to hardcode the URL encoded version, you can use

encodeURIComponent("\n") // %0A

P.S. Wouldn't it be better not to rely on the user's mail client being configured? Use your own server to send the email instead to make sure all users can send a message.

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • This is the correct way to do it.. I can see the spring break is all ready slowing me down :) – Andreas Louv Apr 17 '14 at 19:35
  • I also have another small question... if I want to put the url there in a hyperlink to that url, how do I do that? For some reason, the link isn't working... – Liam Shalon Apr 17 '14 at 19:53
  • @LiamShalon Please create a separate question. At SO, we like questions to be about a single thing. That way, the question is more useful to others. Feel free to link to it here and tag me on it so I'm notified. I think I know the answer but it will be much easier when you create a new question showing what you've tried. – Ruan Mendes Apr 17 '14 at 21:03
3

Try

<br>

instead of

 \n

\n is rendered as 1 space in html and so is \t or \s

E_X
  • 3,722
  • 1
  • 14
  • 15
Cute_Ninja
  • 4,742
  • 4
  • 39
  • 63
  • This won't work with text based email clients – Ruan Mendes Apr 17 '14 at 19:32
  • it works with %0D instead of \n – Jonathan Anctil Apr 17 '14 at 19:35
  • Actually this won't work at all because [the specification for mailto](http://www.ietf.org/rfc/rfc2368.txt) says that it's to be used with text based email only, therefore, the
    would be rendered as is, instead of as a line break. `The "body" hname should contain the content for the first text/plain body part of the message.`
    – Ruan Mendes Apr 17 '14 at 23:30