简体   繁体   中英

How to put linebreaks on dynamically generated javascript code

Working on ASP.net website. In my webpage I'm dynamically writing some javascript in the code-behind code. How can I put linebreaks at certain points so that if I do a 'view source' of the page it is more readable? Right now all the dynamic js code is one one line.

I've tried appending <BR/> but that doesn't seem to work.

Thanks!

<br /> is html element which will not help you at this point. you can use Environment.NewLine to add these line-breaks: example code will be like the following:

StringBuilder strBuilderJS = new StringBuilder(); 
strBuilderJS.Append("<script language='javascript' type='text/javascript'>" + Environment.NewLine + 
                    "$(WireEvents_" + this.ID + ");" + Environment.NewLine +
                    "function WireEvents_" + this.ID + "(){" + Environment.NewLine +
                    "    alert('stuff');");
strBuilderJS.Append(Environment.NewLine + "}</script>");

If you're using Response.Write() , you can use \\r\\n to add a newline in your HTML output.

Example:

Response.Write("<div>\r\nThis is my text.\r\n</div>"

This will appear in the HTML as:

<div>
This is my text.
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM