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/[email protected] \n" +
        "\n Enjoy")
});
6
  • 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. Commented Apr 17, 2014 at 19:24
  • 1
    this might answer your question stackoverflow.com/questions/1155678/… Commented Apr 17, 2014 at 19:24
  • @NULL "\\n" doesn't seem to be working... do you mean replacing "\n" with "\\n"? Commented Apr 17, 2014 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? Commented Apr 17, 2014 at 19:28
  • Try %0D instead of \n Commented Apr 17, 2014 at 19:28

2 Answers 2

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:[email protected]?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/[email protected]  %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.

Sign up to request clarification or add additional context in comments.

3 Comments

This is the correct way to do it.. I can see the spring break is all ready slowing me down :)
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...
@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.
3

Try

<br>

instead of

 \n

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

3 Comments

This won't work with text based email clients
it works with %0D instead of \n
Actually this won't work at all because the specification for mailto says that it's to be used with text based email only, therefore, the <br> 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.