Link to home
Start Free TrialLog in
Avatar of volking
volking

asked on

Javascript equilivent for HttpUtility UrlDecode and UrlEncode

I'm VERY new to Javascript. We're building a corporate application and can guarantee IE 5.5 on the client side, so, we're using C# and ASP.NET on the server side, but have found Javascript is the ONLY SOLUTION for certain Client-Side activities.

Question: Using Javascript ... how do I encode a string such that the Server-Side HttpUtility.UrlDecode() can return it to it's original condition?

... and ... vice-a-versa!
Avatar of Nushi
Nushi

do u mean escape(String)?
escape conver this to url encoding
Nushi.
and the opposite is unescape(String)
use escape/unescape to encode/decode string

Regards
Hart
oh i am late :-)

Regards
Hart
ASKER CERTIFIED SOLUTION
Avatar of Nushi
Nushi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of volking

ASKER

How about a three line function showing the javascript to use escape(String) and unescape(String) .... as I said, I and VERY new to javascript .... (smile)
Avatar of volking

ASKER

My concern is the server side ... can escape()/unescape() be fully reversed on the server side by HttpUtility.UrlDecode() and HttpUtility.UrlEncode()

Thanks
Its not clear to me what u mean?
i posted you a sample code above.

Nushi.
Avatar of devic
escape and unescape Functions
The escape and unescape functions let you encode and decode strings. The escape function returns the hexadecimal encoding of an argument in the ISO Latin character set. The unescape function returns the ASCII string for the specified hexadecimal encoding value.

The syntax of these functions is:

escape(string)unescape(string)
These functions are used primarily with server-side JavaScript to encode and decode name/value pairs in URLs.

read some info about escape unescape
http://www.ods.com.ua/win/eng/web-tech/js/htm/16-03.phtml
Yep.

its a conversion accepted by all.
it follows the standars.

it simply replace all problemtic chars with a "good" ones.

Nushi.
if you arent convinced write a string in JAVA/C++ and encode it.
print the result and compare it to the one you get from the JS function.

you will see that teh are identical.
If you are using IE 5.5, then I would suggest using encodeURIComponent() and decodeURIComponent().  escape() and unescape are deprecated and do not encode certain characters (like + and &).

Example (to encode everything on a form):

function formToQuery( form ) {
      var queryString = '';
      var len = form.elements.length;
      var inited = false;
      var ele = null;
      for ( i = 0; i < len; i++ ) {
            ele = form.elements[i];
            if ( ele.type && ele.type=='radio' && !ele.checked ) continue;
            v = ele.value;
            n = ele.name;
            if ( v && ( n && n.length > 0 ) ) {
                  queryString += ( !inited ) ? '?' : '&';
                  queryString += n + '=' + encodeURIComponent( v );
                  inited = true;
            }
      }

      return queryString;
}
Thank you.
Nushi.
Avatar of volking

ASKER

Tried it and it works as advertised! When one is learning ... even the most simple tasks can be challenging.

Nushi ... thanks!

NP.
if you still have any other question please feel free to ask me.

the excape/unescape functions are used mostly in cokies (not only but mostly).
that the way to store/retrive cookie.

Nushi.