• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

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>
0
Abdu_Allah
Asked:
Abdu_Allah
2 Solutions
 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now