Javascript String in C # Fault-Free

advertisements

I am having a problem with javascript.

The javascript gets created dynamically on a C# winforms project as a string.

The actual javascript code gets generated fine and all the double quotes get escaped as they should. My problem comes in when I assign a string inside of this function such as

string js = "eval(\"var someVar = 'someValue'\");";

So I end up with a string in the browser as

eval(\"var someVar = 'someValue'\");

The problem is the "\" that is escaping the " in front of 'someVar'. It gets put in automatically in C#. That makes the string invalid when trying to execute the actual function in a browser. Any ideas how I might go about solving this?

Thanks.


You can check HttpUtility.JavaScriptStringEncode Method if you are using .net 4 or later framework