unescape() not paired up with escape()

  • Thread starter Thread starter Lau Lei Cheong
  • Start date Start date
L

Lau Lei Cheong

Hello,

I'm writing an application that'll use escape() to encode string and
pass to server-side, than use Urldecode() to regenerate origional strings.
In this fashion I can get rid of the false alarm about invalid characters
that's raised by certain multi-byte characters.

It happens that my javascript will also use the encoded string.But when
I feed the "%XX%XX..." string to the unescape(), it returns garbage because
it treats each "%XX" as distinct characters. and unescape() for unicode
character won't do because it only accept characters in "%uXXXX" format
while my characters should be "%uXXXXXX".

Anyway I can "decrypt" my encoded string? Please suggest them for me.
Thanks a lot.

Regards,
Lau Lei Cheong
 
Lau Lei Cheong wrote:

I'm writing an application that'll use escape() to encode string and
pass to server-side, than use Urldecode() to regenerate origional strings.
In this fashion I can get rid of the false alarm about invalid characters
that's raised by certain multi-byte characters.

It happens that my javascript will also use the encoded string.But when
I feed the "%XX%XX..." string to the unescape(), it returns garbage because
it treats each "%XX" as distinct characters. and unescape() for unicode
character won't do because it only accept characters in "%uXXXX" format
while my characters should be "%uXXXXXX".

When using client-side JScript in IE then I have no problem with the
following example:

var euroSymbol = '€';
var euroSymbolEscaped = escape(euroSymbol);
var unescapedEuroSymbol = unescape(euroSymbolEscaped);
'orginal: "' + euroSymbol + '"; escaped: "' + euroSymbolEscaped +
'"; unescaped: "' + unescapedEuroSymbol + '"'

it yields

orginal: "€"; escaped: "%u20AC"; unescaped: "€"

so the escaped string is correctly unescaped.

Can you post an example where that doesn't work for you with IE/JScript?
 
My mistake.

I was using a function copied from internet like this:
function str_encode(inputstr) {
try
{
outstr = encodeURIComponent(inputstr);
}
catch(e)
{
outstr = escape(inputstr);
}
return outstr;
}

It does exactly what I wanted, "Convert a string into %XX%XX... format so I
can use HttpUtility.UrlDecode() at server-side later". Because of some
wrong comment, I was put into believe that in IE it's the escape part acting
at the mentioned case.

So the question should be pointed to encodeURIComponent() and
decodeURIComponent() instead.

Sorry for not clarifying the problem first.
 
Lau said:
So the question should be pointed to encodeURIComponent() and
decodeURIComponent() instead.

So for what string example does
decodeURIComponent(encodeURIComponent(string))
not yield the original string then?
 
Yes.

encodeURIComponent() does a good job in converting the Chinese characters in
triple-byte Unicode hex-string,
but decodeURIComponent() simply interpret them as 3 maybe single-byte ASCII
character.

For example:
javascript:window.alert(str_decode(%e6%88%90%e5%8a%9f"));

The above example shuold yield "??" in Chinese, but you can see the result
if you type them in IE's address bar.
 
Back
Top