Machado Machado - 1 year ago 74
Javascript Question

Caveats Encoding a C# string to a Javascript string

I'm trying to write a custom Javascript MVC3 Helper class foe my project, and one of the methods is supposed to escape C# strings to Javascript strings.

I know C# strings are UTF-16 encoded, and Javascript strings also seem to be UTF-16. No problem here.

I know some characters like backslash, single quotes or double quotes must be backslash-escaped on Javascript so:

\ becomes \\
' becomes \'
" becomes \"

Is there any other caveat I must be aware of before writing my conversion method ?

Great answers so far, I'm adding some references from the answers in the question to help others in the future.

Alex K. suggested using
, which I marked as the right answer for me, because I'm using .Net 4. But this function is not available to previous .Net versions, so I'm adding some other resources here:

CR becomes \r // Javascript string cannot be broke into more than 1 line
LF becomes \n // Javascript string cannot be broke into more than 1 line
TAB becomes \t

Control characters must be Hex-Escaped

JP Richardson gave an interesting link informing that Javascript uses UCS-2, which is a subset of UTF-16, but how to encode this correctly is an entirely new question.

LukeH on the comments below reminded the CR, LF and TAB chars, and that reminded me of the control chars (BEEP, NULL, ACK, etc...).

Answer Source

(.net 4) You can;

System.Web.HttpUtility.JavaScriptStringEncode(@"aa\bb ""cc"" dd\tee", true);
"aa\\bb \"cc\" dd\\tee"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download