How do you put in a new line into a JavaScript alert box?
问题:
回答1:
\n
will put a new line in - \n
being a control code for new line.
alert("Line 1\nLine 2");
回答2:
alert("some text\nmore text in a new line");
Output:
some text
more text in a new line
回答3:
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 "";
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 "";
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 "";
回答4:
In C# I did:
alert('Text\\n\\nSome more text');
It display as:
Text
Some more text
回答5:
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
回答6:
Just to inform, the \n only works with double quotes.
回答7:
alert("text " + '\n' + "new Line Text");
回答8:
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(''); // not working window.alert(''); // is working
回答9:
alert('The transaction has been approved.\nThank you');
Just add a newline \n character.
alert('The transaction has been approved.\nThank you'); // ^^
回答10:
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);
回答11:
Works with \n
but if the script is into a java tag you must write \\\n
or
回答12:
alert("some text\nmore text in a new line");
alert("Line 1\nLine 2\nLine 3\nLine 4\nLine 5");
回答13:
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");
回答14:
\n
won't work if you're inside java code though:
I know its not an answer, just thought it was important.
回答15:
use the new line character of a javascript instead of '\n'.. eg: "Hello\nWorld" use "Hello\x0AWorld" It works great!!
回答16:
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)");
回答17:
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
回答18:
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.