Javascript equilivent for HttpUtility UrlDecode and UrlEncode

volking
volking used Ask the Experts™
on
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!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
do u mean escape(String)?
escape conver this to url encoding
Nushi.
and the opposite is unescape(String)

Commented:
use escape/unescape to encode/decode string

Regards
Hart
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
oh i am late :-)

Regards
Hart
Commented:
as i said in my firsts comment:
here is a sample code:

<script>
var a= 'this is my String';
alert (escape(a));
alert (unescape( escape(a)));

</script>

Nushi.

Author

Commented:
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)

Author

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

Thanks

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

Nushi.
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

Commented:
Yep.

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

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

Nushi.

Commented:
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.

Commented:
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;
}

Commented:
Thank you.
Nushi.

Author

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

Nushi ... thanks!

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial