admin管理员组文章数量:1415484
I'm outputting client side function calls from code behind via
Page.ClientScript.RegisterStartupScript( this.GetType(), "supportItems", scriptCalls, true );
scriptCalls contains call(s) to a client side function that has several string arguments taken from database and that then displays the parameters in HTML textareas, so line breaks need to ultimately be preserved. If the DB value includes a linebreak then the linebreak gets included in the outputted client script which of course then causes a client script error.
I have tried passing the DB values through a cleaning function to replace line breaks ala:
private string CleanJavaScriptString( string stringToClean )
{
string cleanString = stringToClean.Replace( "'", "\'" );
cleanString = cleanString.Replace( Environment.NewLine, "\n" );
return cleanString;
}
But this still outputs the actual line break in the code. How can I achieve this?
I'm outputting client side function calls from code behind via
Page.ClientScript.RegisterStartupScript( this.GetType(), "supportItems", scriptCalls, true );
scriptCalls contains call(s) to a client side function that has several string arguments taken from database and that then displays the parameters in HTML textareas, so line breaks need to ultimately be preserved. If the DB value includes a linebreak then the linebreak gets included in the outputted client script which of course then causes a client script error.
I have tried passing the DB values through a cleaning function to replace line breaks ala:
private string CleanJavaScriptString( string stringToClean )
{
string cleanString = stringToClean.Replace( "'", "\'" );
cleanString = cleanString.Replace( Environment.NewLine, "\n" );
return cleanString;
}
But this still outputs the actual line break in the code. How can I achieve this?
Share Improve this question asked Jul 11, 2012 at 12:07 Stewart AlanStewart Alan 1,6435 gold badges25 silver badges51 bronze badges2 Answers
Reset to default 3If you have access to it (.NET 3.5 onwards), the best bet it to use JavaScriptSerializer
...
Private Sub MesgBox(ByVal sMessage As String)
Dim serializer as New System.Web.Script.Serialization.JavaScriptSerializer()
Dim msgedtble As String = serializer.Serialize(sMessage)
Page.ClientScript.RegisterStartupScript(Me.GetType, "myScripts",
"<script type='text/javascript'>alert(" & msgedtble & ");</script>")
End Sub
This is taken from the this question/answer.
The advantage of the JavaScriptSerializer is that it will deal with quotes, newlines - and all the characters you might not have thought about, which would affect JavaScript.
EDIT
And here is a C# equivalent to what you're asking for...
private string CleanJavaScriptString( string stringToClean )
{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new
System.Web.Script.Serialization.JavaScriptSerializer();
return serializer.Serialize(stringToClean);
}
you need to double escape the line break so...
private string CleanJavaScriptString( string stringToClean )
{
string cleanString = stringToClean.Replace( "'", "\'" );
cleanString = cleanString.Replace( Environment.NewLine, "\\n" );
return cleanString;
}
本文标签: cHow to deal with linebreaks when outputting Javascript from code behindStack Overflow
版权声明:本文标题:c# - How to deal with linebreaks when outputting Javascript from code behind - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745233956a2648946.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论