How do you put in a new line into a JavaScript alert box?
This question is related to
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.
use the new line character of a javascript instead of '\n'.. eg: "Hello\nWorld" use "Hello\x0AWorld" It works great!!
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>";
You can use \n for new line
alert("Welcome\nto Jumanji");
alert("Welcome\nto Jumanji");
_x000D_
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);
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
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
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)");
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
alert("some text\nmore text in a new line");
_x000D_
alert("Line 1\nLine 2\nLine 3\nLine 4\nLine 5");
_x000D_
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");
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');" />
In C# I did:
alert('Text\\n\\nSome more text');
It display as:
Text
Some more text
alert("some text\nmore text in a new line");
_x000D_
Output:
some text
more text in a new line
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.
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
alert('The transaction has been approved.\nThank you');
_x000D_
Just add a newline \n character.
alert('The transaction has been approved.\nThank you');
// ^^
As of ECMAScript 2015 you can use back-ticks (` `) to enclose Template Literals for multi-line strings like this:
alert(`Line1_x000D_
Line2`);
_x000D_
Outputs:
Line1
Line2
Source: Stackoverflow.com