22

Please let me know how to break line in JavaScript.

<input type='submit' name='Submit' value='Submit' 
onClick="parent.location='mailto:er.saurav123@gmail.com?subject=Thanks for writing to me &body=I will get back to you soon. Thanks and Regards Saurav Kumar'">

I want a break line in Subject. The output I need is:

I will get back to you soon
Thanks and Regards
Saurav Kumar
halfer
  • 19,471
  • 17
  • 87
  • 173
saurav2109
  • 241
  • 1
  • 3
  • 8

5 Answers5

27

Add %0D%0A to any place you want to encode a line break on the URL.

  • %0D is a carriage return character
  • %0A is a line break character

This is the new line sequence on windows machines, though not the same on linux and macs, should work in both.

If you want a linebreak in actual javascript, use the \n escape sequence.


onClick="parent.location='mailto:er.saurav123@gmail.com?subject=Thanks for writing to me &body=I will get back to you soon.%0D%0AThanks and Regards%0D%0ASaurav Kumar'
Oded
  • 477,625
  • 97
  • 867
  • 998
  • 1
    @saurav2109 - Add an additional `%0D%0A` for every linebreak. – Oded Jan 22 '11 at 13:42
  • @oded:Thanks a lot. I have one more query. I have one HTML page with 2 text box, 1 dropdown and 1 button, on button click I want to open the email(as currently I am doing with javascript) with subject line as whatever is selected in the drop down and whatever is written in text box. – saurav2109 Jan 22 '11 at 14:04
  • I want the subject should be: Number | Unit | Dropdown Selected value. Please have a look at the code below – saurav2109 Jan 22 '11 at 14:07
  • @Oded: Yes, Please help in this aspect – saurav2109 Jan 22 '11 at 14:08
  • @saurav2109 - Please ask a new question for this. Comments are not a place for asking new questions. – Oded Jan 22 '11 at 14:08
  • Emp Number:

    Unit Name:

    Type of subscription you want:

    – saurav2109 Jan 22 '11 at 14:13
  • 4
    @saurav2109 - I will say it one last time. I will not answer this new question here. **ask a new question**. – Oded Jan 22 '11 at 14:14
  • @Oded: I have posted new question. Sorry about that. Please help in that aspect. – saurav2109 Jan 22 '11 at 14:18
  • @saurav2109, please also accept this answer (the tick on the left) if it helped you. – David Tang Jan 22 '11 at 14:19
  • @saurav2109 - You can only select _one_ question as accepted. – Oded Jan 22 '11 at 14:21
  • @Oded: I had accepted your answer. Also I had posted one more question. Please have a look and do the needful. – saurav2109 Jan 22 '11 at 14:25
  • 4
    @saurav2109 - I answer questions when I think I have something to add. There are many users here. You do not need to ask a particular one for help... just be patient. – Oded Jan 22 '11 at 14:30
22

Here you are ;-)

<script type="text/javascript">
    alert("Hello there.\nI am on a second line ;-)")
</script>
eckes
  • 9,711
  • 1
  • 55
  • 69
Andreyco
  • 21,872
  • 4
  • 59
  • 64
12

I was facing the same problem. For my solution, I added br enclosed between 2 brackets < > enclosed in double quotation marks, and preceded and followed by the + sign:

+"<br>"+

Try this in your browser and see, it certainly works in my Internet Explorer.

4444
  • 3,533
  • 10
  • 29
  • 43
Haresh Narang
  • 121
  • 1
  • 3
10
alert("I will get back to you soon\nThanks and Regards\nSaurav Kumar");

or use %0D%0A in a url

mplungjan
  • 155,085
  • 27
  • 166
  • 222
-1

Using + "<br>" + between each point you want to break may work for you.

e.g.

var numberTimes = 5;

for(var times = 0; times < numberTimes; times++) {
    document.write("Number of times executed: " + times + " " + "<br>");
}

document.write("It's over.")