Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# alternative for javascript escape function

Tags:

asp.net

what is an alternative for javascript escape function in c# for e.g suppose a string:"Hi Foster's i'm missing /you" will give "Hi%20Foster%27s%20i%27m%20missing%20/you" if we use javascript escape function, but what is the alternative for c#. i have searched for it but no use.

like image 381
FosterZ Avatar asked Mar 23 '10 12:03

FosterZ


3 Answers

You can use:

string encoded = HttpUtility.JavaScriptStringEncode(str);

Note: You need at least ASP.NET 4.0 to run the above code.

like image 99
Prakash Avatar answered Oct 21 '22 10:10

Prakash


var unescapedString = Microsoft.JScript.GlobalObject.unescape(yourEscapedString);

var escapedString = Microsoft.JScript.GlobalObject.escape(yourUnescapedString);
like image 7
Volodymyr Avatar answered Oct 21 '22 11:10

Volodymyr


The best solution I've seen is mentioned on this blog - C#: Equivalent of JavaScript escape function by Kaushik Chakraborti. There is more to escaping javascript than simply url-encoding or replacing spaces with entities.

like image 6
Dan Diplo Avatar answered Oct 21 '22 11:10

Dan Diplo