26

Please let me know how to break line in JavaScript.

<input type='submit' name='Submit' value='Submit' 
onClick="parent.location='mailto:[email protected]?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
1
  • 1
    %0d%0a is very intereting.
    – A.J.
    Feb 6, 2019 at 6:32

5 Answers 5

30

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:[email protected]?subject=Thanks for writing to me &body=I will get back to you soon.%0D%0AThanks and Regards%0D%0ASaurav Kumar'
7
  • 1
    @saurav2109 - Add an additional %0D%0A for every linebreak.
    – Oded
    Jan 22, 2011 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, 2011 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, 2011 at 14:07
  • @Oded: Yes, Please help in this aspect
    – saurav2109
    Jan 22, 2011 at 14:08
  • 4
    @saurav2109 - I will say it one last time. I will not answer this new question here. ask a new question.
    – Oded
    Jan 22, 2011 at 14:14
22

Here you are ;-)

<script type="text/javascript">
    alert("Hello there.\nI am on a second line ;-)")
</script>
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.

0
10
alert("I will get back to you soon\nThanks and Regards\nSaurav Kumar");

or use %0D%0A in a url

2
  • %0D%0A is single line break, how to make 2 line break
    – saurav2109
    Jan 22, 2011 at 13:43
  • 7
    You just repeat it twice: %0D%0A%0D%0A.
    – Luna
    Jan 22, 2011 at 13:45
-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.")

1
  • 2
    It won't. Pay attention to the context of the question. It's a URL not HTML.
    – Quentin
    Aug 31, 2021 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.