18

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
  • 18,701
  • 13
  • 79
  • 158
saurav2109
  • 201
  • 1
  • 3
  • 8

4 Answers4

23

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
  • 463,167
  • 92
  • 837
  • 979
  • @saurav2109 - I got it wrong first time, using decimal instead of hex values. – Oded Jan 22 '11 at 13:40
  • @Oded: Thanks a lot, its working fine. but how to make 2 line break – saurav2109 Jan 22 '11 at 13:42
  • 1
    @saurav2109 - Add an additional `%0D%0A` for every linebreak. – Oded Jan 22 '11 at 13:42
  • @Oded: Awesome..Thanks a lot, can you tell me is there any way to send email from an HTMl form – saurav2109 Jan 22 '11 at 13:46
  • @saurav2109 - You should use server side technology for that. PHP, perl, asp.net, ruby are all examples of server side scripting languages. – Oded Jan 22 '11 at 13:47
  • @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
17

Here you are ;-)

<script type="text/javascript">
    alert("Hello there.\nI am on a second line ;-)")
</script>
eckes
  • 9,350
  • 1
  • 52
  • 65
Andreyco
  • 20,909
  • 4
  • 54
  • 61
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,523
  • 10
  • 27
  • 43
Haresh Narang
  • 121
  • 1
  • 3
9
alert("I will get back to you soon\nThanks and Regards\nSaurav Kumar");

or %0D%0A in a url

mplungjan
  • 134,906
  • 25
  • 152
  • 209