Using C# for ASP.NET and MOSS development, we often have to embed JavaScript into our C# code. To accomplish this, there seems to be two prevalent schools of thought:
string blah = "asdf";
StringBuilder someJavaScript = new StringBuilder();
someJavaScript.Append("<script language='JavaScript' >");
someJavaScript.Append("function foo()\n");
someJavaScript.Append("{\n");
someJavaScript.Append(" var bar = '{0}';\n", blah);
someJavaScript.Append("}\n");
someJavaScript.Append("</script>");
The other school of thought is something like this:
string blah = "asdf";
string someJavaScript = @"
<script language='JavaScript' >
function foo()
{
var bar = '" + blah + @"';
}
</script>";
Is there a better way than either of these two methods? I like the second personally, as you can see the entire section of JavaScript (or other language block, whether SQL or what have you), and it also aids in copying the code between another editor for that specific language.
Edit:
I should mention that the end goal is having formatted JavaScript in the final web page.
I also changed the example to show interaction with the generated code.