notes on javascript, linux, and more

1.15.2009

Escaping characters between Javascript and .NET

In .NET, the most robust escape function for special characters is Server.UrlEncode. In Javascript, it is said that this is compatible with encodeURIComponent. However, I found two discrepancies: the "~" character and the space character. Here's a Javascript snippet you can use to escape things in a manner compatible with .NET:
urlDecode = function (text) {
/*
description:
- javascript equivalent of .NET's Server.UrlDecode
*/
return decodeURIComponent(text.replace(/\+/g,'%20').replace(/%7e/g,'~'));
}
urlEncode = function (text) {
/*
description:
- javascript equivalent of .NET's Server.UrlEncode
*/
return encodeURIComponent(text).replace(/%20/g,'+').replace(/~/g,'%7e');
}

About Me

My photo
chicago, il, United States
I'm a software engineer by profession.

Labels