Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

line break in alert popup

I am trying to add a new line Javascript alert message. I tried '\n' and 'Environment.NewLine'. I am getting Unterminated string constant error. Could you please let me know what could be the problem? I appreciate any help. I also tried \r\n.

string msg = "Your session will expire in 10 minutes. \n Please save your work to avoid this.";


if (!this.ClientScript.IsStartupScriptRegistered(ID)) 
   this.ClientScript.RegisterStartupScript(GetType(), ID, String.Format("<script language=JavaScript>setTimeout(\'alert(\"{1}\");\',{0}*1000);</script>", sTime, msg));
like image 750
nav100 Avatar asked Sep 26 '12 14:09

nav100


People also ask

How do I put a line break in an alert message?

To add a new line to the content of alert box we are going to use \n backslash(n). Example-1: This example adds \n between the 2 lines to the text of alert box.

How do I pop an alert in HTML?

The Window alert() method is used to display an alert box. It displays a specified message along with an OK button and is generally used to make sure that the information comes through the user. It returns a string which represents the text to display in the alert box.

How do you insert a line break in JavaScript?

The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.

How do you add inputs to an alert?

You can't put anything in an alert box. As the name indicates, it's an alert. You might be looking for a prompt which has an input text field, or confirm to get a true / false depending on user selection.


1 Answers

I would suspect that you need to change your code to;

string msg = "Your session will expire in 10 minutes. \\n Please save your work to avoid this.";

And escape the \n otherwise your code outputted would actually include the line break rather than \n

Your output code would look like:

setTimeout('alert("Your Session....
 Please save your work to ....");', 1000);

Rather than:

setTimeout('alert("Your Session....\n Please save your work to ....");', 1000);
like image 58
Tim B James Avatar answered Sep 25 '22 18:09

Tim B James