Best way to make alert messages safe in javascript
<p>I want to display messages from my asp.net pages by registering a script, sometimes even the exception code from an error.  I use this code:</p>

<pre><code>ScriptManager.RegisterStartupScript(this, Page.GetType(), "NoDept", "alert('Adjustments require the Total Amount to be ZERO to post!');", true);
</code></pre>

<p>This works great if I can decide the information to display during development, but if I want to display something that is data from the application, it may include invalid text, which of course screws up the script and it does not display.  An example would be something with ' in the message.</p>

<p>I am running into the problem when I try to display a message from an exception such as:</p>

<pre><code>ScriptManager.RegisterStartupScript(this, Page.GetType(), "processError", "alert('" + ptcred.TransactionError + "');", true);
</code></pre>

<p>The ptcred.TransactionError is the ASP.NET Exception error, and it may contain invalid characters.</p>

<p>What would be the best way to handle presenting this information in my alert() function that will not screw up my javascript?  Possibly something that jQuery could do to clean it up?</p>

<p>EDIT:  I should explain, I would like to continue to easily just register  an alert script, its easy and convenient.  Just wondered if there wsa something like HTML.Encode or something that will clean up infromation that will make a message not screw up the script.</p>

<p>Thanks,
Cory</p>
