简体   繁体   中英

New line in JavaScript alert box

如何在 JavaScript 警告框中添加新行?

\\n将放入一个新行 - \\n是新行的控制代码。

 alert("Line 1\\nLine 2");

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

输出:

some text
more text in a new line

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>";

In C# I did:

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

It display as:

Text

Some more text

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
alert("text\nnew Line Text");

Documentation: Window.alert()


Firefox:
火狐演示

Chrome:
铬演示

Edge:
边缘演示

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

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');" />

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

Just add a newline \\n character.

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

以防万一这对任何人有帮助,当从后面的 C# 代码执行此操作时,我必须使用双转义字符,否则会出现“未终止的字符串常量”JavaScript 错误:

ScriptManager.RegisterStartupScript(this, this.GetType(), "scriptName", "alert(\"Line 1.\\n\\nLine 2.\");", true);

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

 alert("Line 1\\nLine 2\\nLine 3\\nLine 4\\nLine 5");

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

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");

\\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.

使用 javascript 的换行符而不是 '\\n' .. 例如:“Hello\\nWorld”使用“Hello\\x0AWorld”它很好用!!

您可以使用\\ n \\添加新行

 alert('Deleting.. \n\ This action is irreversible!');

您可以使用\\n换行

alert("Welcome\nto Jumanji");

 alert("Welcome\\nto Jumanji");

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)");

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM