Nueva línea en el cuadro de alerta de JavaScript
¿Cómo poner una nueva línea en un cuadro de alerta de JavaScript?
18 answers
\n
pondrá una nueva línea en - \n
siendo un código de control para la nueva línea.
alert("Line 1\nLine 2");
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-12 21:23:50
alert("some text\nmore text in a new line");
Salida:
some text
more text in a new line
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-15 12:16:59
Tienes que usar comillas dobles para mostrar caracteres especiales como \n \ t, etc... en el cuadro de alerta js por ejemplo en script php:
$string = 'Hello everybody \n this is an alert box';
echo "<script>alert(\"$string\")</script>";
Pero un segundo posible problema llega cuando desea mostrar una cadena especificada entre comillas dobles.
Ver texto de enlace
Si la cadena está encerrada entre comillas dobles ( " ), PHP interpretará más secuencias de escape para caracteres especiales
Secuencias de escape \n se transforma como 0x0A ASCII carácter Escapado y este carácter es no se muestra en el cuadro de alerta. La solución consiste en escapar de esta secuencia especial:
$s = "Hello everybody \\n this is an alert box";
echo "<script>alert(\"$string\")</script>";
Si no sabe cómo está encerrada la cadena, debe transformar caracteres especiales en sus secuencias de escape
$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>";
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-08-30 15:09:27
En C # lo hice:
alert('Text\\n\\nSome more text');
Se muestra como:
Texto
Un poco más de texto
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-05 18:20:38
Lista de códigos de Caracteres Especiales en JavaScript:
Code Outputs
\' single quote
\" double quote
\\ backslash
\n new line
\r carriage return
\t tab
\b backspace
\f form feed
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-02-15 01:55:54
Solo para informar, \n solo funciona con comillas dobles.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-12-03 19:34:11
alert("text " + '\n' + "new Line Text");
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-03 14:38:09
Cuando desea escribir una alerta javascript desde una variable php, debe agregar otro "\" antes de "\n". En su lugar, la ventana emergente de alerta no funciona.
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
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-16 09:00:21
Funciona con \n
pero si el script está en una etiqueta java debe escribir \\\n
<script type="text/javascript">alert('text\ntext');</script>
O
<h:commandButton action="#{XXXXXXX.xxxxxxxxxx}" value="XXXXXXXX"
onclick="alert('text\\\ntext');" />
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-15 07:32:18
alert('The transaction has been approved.\nThank you');
Simplemente agregue un carácter \n de nueva línea.
alert('The transaction has been approved.\nThank you');
// ^^
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-12 14:48:26
En caso de que esto ayude a alguien, al hacer esto desde el código C # detrás tuve que usar un carácter de escape doble o obtuve una" constante de cadena sin terminar " Error de JavaScript:
ScriptManager.RegisterStartupScript(this, this.GetType(), "scriptName", "alert(\"Line 1.\\n\\nLine 2.\");", true);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-01 15:19:49
alert("some text\nmore text in a new line");
alert("Line 1\nLine 2\nLine 3\nLine 4\nLine 5");
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-25 13:21:11
Gracias por las sugerencias. Usar el signo " + " es la única forma de que funcione. Esta es la última línea de una función que agrega algunos números. Estoy aprendiendo JavaScript:
alert("Line1: The sum is " + sum + "\n" + "Line 2");
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-04 19:40:00
\n
no funcionará si estás dentro de código java:
<% System.out.print("<script>alert('Some \n text')</script>"); %>
Sé que no es una respuesta, solo pensé que era importante.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-23 16:51:35
Utilice el nuevo carácter de línea de un javascript en lugar de '\n'.. por ejemplo: "Hello\nWorld" use " Hello\x0AWorld" ¡Funciona muy bien!!
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-12 23:29:27
Vi que algunas personas tenían problemas con esto en MVC, así que... una forma sencilla de pasar '\n' usando el Modelo, y en mi caso incluso usando un texto traducido, es usar HTML.Raw para insertar el texto. Eso me lo arregló. En el siguiente código, Modelo.La lata de alertas contiene nuevas líneas, como "Hello \ nWorld"...
alert("@Html.Raw(Model.Alert)");
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-02 20:39:21
Usé: "\n\r" - aunque solo funciona entre comillas dobles.
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
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-22 18:33:18
Se puede lograr un nuevo carácter de línea en javascript usando \n
Esto se puede hacer usando
alert("first line \n second line \n third line");
Salida:
Primera línea
Segunda línea
Tercera línea
Aquí hay un jsfiddle preparado para el mismo.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-03 14:45:50