Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a standard way to encode a .NET string into JavaScript string for use in MS Ajax?

I'm trying to pass the output of a SQL Server exception to the client using the RegisterStartUpScript method of the MS ScriptManager in .NET 3.5. This works fine for some errors but when the exception contains single quotes the alert fails.

I dont want to only escape single quotes though. Is there a standard function I can call to escape any special chars for use in JavaScript?

string scriptstring = "alert('" + ex.Message + "');";          ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", scriptstring , true); 

EDIT:

Thanks @tpeczek, the code almost worked for me :) but with a slight amendment (the escaping of single quotes) it works a treat.

I've included my amended version here...

public class JSEncode {     /// <summary>     /// Encodes a string to be represented as a string literal. The format     /// is essentially a JSON string.     ///      /// The string returned includes outer quotes      /// Example Output: "Hello \"Rick\"!\r\nRock on"     /// </summary>     /// <param name="s"></param>     /// <returns></returns>     public static string EncodeJsString(string s)     {         StringBuilder sb = new StringBuilder();         sb.Append("\"");         foreach (char c in s)         {             switch (c)             {                 case '\'':                     sb.Append("\\\'");                     break;                 case '\"':                     sb.Append("\\\"");                     break;                 case '\\':                     sb.Append("\\\\");                     break;                 case '\b':                     sb.Append("\\b");                     break;                 case '\f':                     sb.Append("\\f");                     break;                 case '\n':                     sb.Append("\\n");                     break;                 case '\r':                     sb.Append("\\r");                     break;                 case '\t':                     sb.Append("\\t");                     break;                 default:                     int i = (int)c;                     if (i < 32 || i > 127)                     {                         sb.AppendFormat("\\u{0:X04}", i);                     }                     else                     {                         sb.Append(c);                     }                     break;             }         }         sb.Append("\"");          return sb.ToString();     } } 

As mentioned below - original source: here

like image 837
Rich Andrews Avatar asked May 27 '10 11:05

Rich Andrews


People also ask

How are strings encoded in JavaScript?

In order to encode/decode a string in JavaScript, We are using built-in functions provided by JavaScript. btoa(): This method encodes a string in base-64 and uses the “A-Z”, “a-z”, “0-9”, “+”, “/” and “=” characters to encode the provided string.

How is JavaScript encoded?

Because Javascript was invented twenty years ago in the space of ten days, it uses an encoding that uses two bytes to store each character, which translates roughly to an encoding called UCS-2, or another one called UTF-16.


1 Answers

Have you had a look at HttpUtility.JavaScriptStringEncode?

like image 141
Russ Cam Avatar answered Oct 11 '22 00:10

Russ Cam