23

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,824
  • 17
  • 99
  • 186
saurav2109
  • 251
  • 1
  • 3
  • 8

5 Answers5

28

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
  • 489,969
  • 99
  • 883
  • 1,009
  • 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
22

Here you are ;-)

<script type="text/javascript">
    alert("Hello there.\nI am on a second line ;-)")
</script>
eckes
  • 10,103
  • 1
  • 59
  • 71
Andreyco
  • 22,476
  • 5
  • 61
  • 65
13

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,541
  • 10
  • 32
  • 43
Haresh Narang
  • 131
  • 1
  • 3
10
alert("I will get back to you soon\nThanks and Regards\nSaurav Kumar");

or use %0D%0A in a url

mplungjan
  • 169,008
  • 28
  • 173
  • 236
-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.")