How to javascript code in a different lines using RegisterStartupScript

Hi; How to javascript code in a different lines using RegisterStartupScript , foexample, consider the following code:
Page.ClientScript.RegisterStartupScript(this.GetType(), "yyh", "<script>alert('1'); alert('2');</script>");

This function will print the code in the web page as the following "<script>alert('1'); alert('2');</script>
How to make it printed like this:

<script>
alert('1');
 alert('2');
</script>
LVL 3
Abdu_AllahAsked:
Who is Participating?
 
guru_samiCommented:
using System.Text;

------>

StringBuilder sb = new StringBuilder();
        sb.Append("<script>");
        sb.AppendLine();
        sb.Append("alert('1');");
        sb.AppendLine();
        sb.Append("alert('2');");
        sb.AppendLine();
        sb.Append("</script>");

Page.ClientScript.RegisterStartupScript(this.GetType(), "yyh", sb.ToString());
0
 
hehdaddyCommented:
You can also insert line breaks:

"<script>\nalert('1');\nalert('2');\n</script>"
0
 
jeyan10Commented:
Hi,

You can use line break command , That is \n as folloes:

"<script>\nalert('1');\nalert('2');\n</script>"

Thanks & Happy New Year!
0
 
Abdu_AllahAuthor Commented:
line breal "\n" does not work.
0
 
guru_samiCommented:
the first one I provided should work.
Still if you want to use \n here it is:
Page.ClientScript.RegisterStartupScript(this.GetType(), "yyh", "<script>\n alert('1');\n alert('2');\n </script>");
Copy+paste this ...it is tested and works.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.