436

How do you put in a new line into a JavaScript alert box?

p.campbell
  • 98,673
  • 67
  • 256
  • 322
Germ
  • 6,360
  • 4
  • 26
  • 26

20 Answers20

663

\n will put a new line in - \n being a control code for new line.

alert("Line 1\nLine 2");
Madness
  • 2,730
  • 3
  • 20
  • 29
Michael Gattuso
  • 13,020
  • 2
  • 25
  • 29
  • 1
    Any ideas on how cross-browser this is? – Ates Goral Dec 03 '09 at 18:22
  • 4
    If I use \n in my alert message, the dialog does not even display in ASP.NET application. Does Microsoft has problem with this :) – TheTechGuy Jul 21 '11 at 12:16
  • 23
    To The crocodile hunter, in ASP.NET behind code alerts, you need to use escape characters, this means Registerblabla(bla,bla,"alert('hi\\nhi second line')") – NicolasT Nov 16 '11 at 14:47
  • 6
    In ASP.NET MVC4, I used `` instead of `\n` to get a new line. – Kevin Meredith Aug 16 '13 at 15:17
  • 1
    In php it is not diplsying alert window if I use; window.alert('Message sent successfully \n It will be reviewed soon') but when I used window.alert('Message sent successfully \\n It will be reviewed soon') everything worked fine for me – Ahmed Syed May 20 '14 at 15:00
  • If you are passing a message to CKEditor from a c# program (and maybe generally in any c# string construction), you have to preface your string by '@' for '\n' to be passed as a newline character. – Jack Herr May 25 '16 at 20:48
  • In ASP.NET MVC4 `\n` works for me. Simple javascript alert(). – Muflix Sep 23 '16 at 11:44
  • ASP.NET is server side so it should not make any difference. In server side processing you should escape special characters in strings. In C# I find the @"..." construct very useful for this. – Martien de Jong Sep 20 '17 at 14:36
  • Contradicts [this blog first google result](https://www.tutorialspoint.com/How-to-Line-Breaks-to-JavaScript-Alert) which says it must be `\r\n` which is ***Windows-Speak*** used in Python on that platform. – WinEunuuchs2Unix Apr 26 '22 at 22:36
52

 alert("some text\nmore text in a new line");

Output:

some text
more text in a new line

juFo
  • 17,849
  • 10
  • 105
  • 142
Amarghosh
  • 58,710
  • 11
  • 92
  • 121
44

you have to use double quotes to display special char like \n \t etc... in js alert box for exemple in php script:

$string = 'Hello everybody \n this is an alert box';
echo "<script>alert(\"$string\")</script>";

But a second possible problem arrives when you want to display a string specified in double quoted.

see link text

If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters

escape sequences \n is transformed as 0x0A ASCII Escaped character and this character is not displayed in the alert box. The solution consists in to escape this special sequence:

$s = "Hello everybody \\n this is an alert box";
echo "<script>alert(\"$string\")</script>";

if you don't know how the string is enclosed you have to transform special characters to their escape sequences

$patterns = array("/\\\\/", '/\n/', '/\r/', '/\t/', '/\v/', '/\f/');
$replacements = array('\\\\\\', '\n', '\r', '\t', '\v', '\f');
$string = preg_replace($patterns, $replacements, $string);
echo "<script>alert(\"$string\")</script>";
greg
  • 457
  • 4
  • 3
30

In C# I did:

alert('Text\\n\\nSome more text');

It display as:

Text

Some more text

Anne
  • 26,765
  • 9
  • 65
  • 71
Paul H
  • 309
  • 3
  • 2
20

List of Special Character codes in JavaScript:

Code    Outputs
\'  single quote
\"  double quote
\\  backslash
\n  new line
\r  carriage return
\t  tab
\b  backspace
\f  form feed
Bishnu Paudel
  • 2,083
  • 1
  • 21
  • 39
18
alert("text\nnew Line Text");

Documentation: Window.alert()


Firefox:
firefox demo

Chrome:
chrome demo

Edge:
edge demo

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
Basem Olimy
  • 767
  • 7
  • 10
9

When you want to write in javascript alert from a php variable, you have to add an other "\" before "\n". Instead the alert pop-up is not working.

ex:

PHP :
$text = "Example Text : \n"
$text2 = "Example Text : \\n"

JS:
window.alert('<?php echo $text; ?>');  // not working
window.alert('<?php echo $text2; ?>');  // is working
Julha
  • 1,107
  • 14
  • 14
  • Why is this answer underrated? if your are thinking way more dynamically in terms of programming webpages this is a big help. – Jimwel Anobong Oct 25 '17 at 12:59
  • When setting a var in PHP with double quotes, PHP checks for variables inside the double quotes. Theoretically, this costs resources. Simple and straightforward way to create js alerttext in PHP, is using single quotes and add variables outside the single qoutes, like $jsAlert=' bla bla '.$var.' \nmore bla bla'; – Terradon Nov 02 '17 at 14:20
  • I agree but you can't ever do like you said. Example use case, when all the string variables come from PHP, you can't write a particular handling for each string that needs a linebreak. – Julha Nov 02 '17 at 20:14
5

 alert("some text\nmore text in a new line");

alert("Line 1\nLine 2\nLine 3\nLine 4\nLine 5");
Abdul Hameed
  • 263
  • 4
  • 19
4

Works with \n but if the script is into a java tag you must write \\\n

<script type="text/javascript">alert('text\ntext');</script>

or

<h:commandButton action="#{XXXXXXX.xxxxxxxxxx}" value="XXXXXXXX" 
    onclick="alert('text\\\ntext');" />
David
  • 3
  • 1
David
  • 73
  • 1
  • Within Java tags you just need to escape it twice. As in `<%out.print("alert('First line\\nSecond line.')");%>` – third_eye Jan 14 '14 at 21:12
4

alert('The transaction has been approved.\nThank you');

Just add a newline \n character.

alert('The transaction has been approved.\nThank you');
//                                       ^^
Muhammad Awais
  • 4,238
  • 1
  • 42
  • 37
  • That's what Michael Gattuso said six and a half years ago. His answer was accepted. This doesn't seem to say anything new, and is basically a copy of that. – Quentin Jun 12 '16 at 14:55
  • @Quentin: Difference is single- vs. double-quotes ... works both ways, which Michael Gattuso didn't mention. – Ole Sauffaus Dec 18 '17 at 14:11
  • @OleSauffaus — That's an irrelevant difference. – Quentin Dec 18 '17 at 14:15
3

use the new line character of a javascript instead of '\n'.. eg: "Hello\nWorld" use "Hello\x0AWorld" It works great!!

Chitra
  • 41
  • 1
3

As of ECMAScript 2015 you can use back-ticks (` `) to enclose Template Literals for multi-line strings like this:

alert(`Line1
Line2`);

Outputs:

Line1
Line2
Geoffrey Hale
  • 10,597
  • 5
  • 44
  • 45
3

Just in case this helps anyone, when doing this from C# code behind I had to use a double escape character or I got an "unterminated string constant" JavaScript error:

ScriptManager.RegisterStartupScript(this, this.GetType(), "scriptName", "alert(\"Line 1.\\n\\nLine 2.\");", true);
Sir Crispalot
  • 4,792
  • 1
  • 39
  • 64
2

Thanks for the hints. Using the "+" sign is the only way I could get it to work. This is the last line of a function that adds some numbers. I'm just learning JavaScript myself:

alert("Line1: The sum is  " + sum + "\n" + "Line 2");
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
2

You can use \n for new line

alert("Welcome\nto Jumanji");

alert("Welcome\nto Jumanji");
Arun Karnawat
  • 575
  • 10
  • 21
1

\n won't work if you're inside java code though:

<% System.out.print("<script>alert('Some \n text')</script>"); %>

I know its not an answer, just thought it was important.

third_eye
  • 428
  • 5
  • 22
-1

A new line character in javascript can be achieved by using \n

This can be done using

alert("first line \n second line \n third line");

Output :

first line

second line

third line

here is a jsfiddle prepared for the same.

Community
  • 1
  • 1
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
-1

I used: "\n\r" - it only works in double quotes though.

var fvalue = "foo";
var svalue = "bar";
alert("My first value is: " + fvalue + "\n\rMy second value is: " + svalue);

will alert as:

My first value is: foo
My second value is: bar
-1

I saw some people had trouble with this in MVC, so... a simple way to pass '\n' using the Model, and in my case even using a translated text, is to use HTML.Raw to insert the text. That fixed it for me. In the code below, Model.Alert can contains newlines, like "Hello\nWorld"...

alert("@Html.Raw(Model.Alert)");
Andy
  • 842
  • 1
  • 12
  • 24
-1

In JAVA app, if message is read from "properties" file.

Due to double compilation java-html,

you'll need double escape.

jsp.msg.1=First Line \\nSecond Line

will result in

First Line
Second Line
BiScOtTiNo
  • 175
  • 11