New line in JavaScript alert box
JavascriptJavascript Problem Overview
How do you put in a new line into a JavaScript alert box?
Javascript Solutions
Solution 1 - Javascript
\n
will put a new line in - \n
being a control code for new line.
alert("Line 1\nLine 2");
Solution 2 - Javascript
alert("some text\nmore text in a new line");
Output:
>some text
more text in a new line
Solution 3 - Javascript
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>";
Solution 4 - Javascript
In C# I did:
alert('Text\\n\\nSome more text');
It display as:
> Text
>
> Some more text
Solution 5 - Javascript
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
Solution 6 - Javascript
Solution 7 - Javascript
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
Solution 8 - Javascript
alert('The transaction has been approved.\nThank you');
Just add a newline \n character.
alert('The transaction has been approved.\nThank you');
// ^^
Solution 9 - Javascript
alert("some text\nmore text in a new line");
alert("Line 1\nLine 2\nLine 3\nLine 4\nLine 5");
Solution 10 - Javascript
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);
Solution 11 - Javascript
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');" />
Solution 12 - Javascript
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
Solution 13 - Javascript
use the new line character of a javascript instead of '\n'.. eg: "Hello\nWorld" use "Hello\x0AWorld" It works great!!
Solution 14 - Javascript
You can use \n for new line
alert("Welcome\nto Jumanji");
alert("Welcome\nto Jumanji");
Solution 15 - Javascript
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");
Solution 16 - Javascript
\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.
Solution 17 - Javascript
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)");
Solution 18 - Javascript
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
Solution 19 - Javascript
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.
Solution 20 - Javascript
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