Escape all characters that need to be escaped
<p>I am using a modal window in my asp.net application. On clicking a button I do some serverside processing and then use cs.RegisterStartupScript to close the window and return a value to the parent window. To do that I use RegisterStartupScript to run window.close. </p>

<p>Now the problem i am having is that I want to send a return value (a string) to the parent. But since the string is a user entered text that can have any characters, I need to escape all of them. Without that I am unable to send the return value to the parent.</p>

<p>This is my script that causes failure (error message : 
unterminated string constant
DO you want to continue running scripts on this page?)</p>

<pre><code>  StringBuilder saveScript = new StringBuilder();
    saveScript.Append("var template = new Object();");
    saveScript.AppendFormat("template.DescriptionPlainText = \"{0}\";",description);
    saveScript.Append("window.returnValue = template;");
    saveScript.Append("window.close();");
    cs.RegisterStartupScript(typeof(myPageType), scriptName, saveScript.ToString(), true);
</code></pre>

<p>description is a string.</p>

<p>However this succeeds</p>

<pre><code>StringBuilder saveScript = new StringBuilder();
saveScript.Append("var template = new Object();");
saveScript.AppendFormat("template.DescriptionPlainText = \"{0}\";","xx");
saveScript.Append("window.returnValue = template;");
saveScript.Append("window.close();");
cs.RegisterStartupScript(typeof(myPageType), scriptName, saveScript.ToString(), true);
</code></pre>
