url encoding function that can't cope with euro signs
Posted: Thu Mar 23, 2006 5:55 am
I've used this javascript function for encoding strings I want to pass via a query string with Ajax as escape() is not adequate. Problem is, (and this is why I try to avoid using other people's code), I haven't got a clue how it does what its doing.
I've just realised that this function can't cope with converting the euro symbol from unicode to hex, but I don't know why, and whether there is anyway to fix it. The function just throws up its standard alert "Unicode character '€' cannot be encoded using standard URL encoding.(URL encoding only supports 8-bit characters.) A space (+) will be substituted."
How can I adapt this code so that it can cope with more than 8-bit chars (and also why is € more than 8-bit?)

I've just realised that this function can't cope with converting the euro symbol from unicode to hex, but I don't know why, and whether there is anyway to fix it. The function just throws up its standard alert "Unicode character '€' cannot be encoded using standard URL encoding.(URL encoding only supports 8-bit characters.) A space (+) will be substituted."
How can I adapt this code so that it can cope with more than 8-bit chars (and also why is € more than 8-bit?)
Code: Select all
function URLEncode(text )
{
// The Javascript escape and unescape functions do not correspond
// with what browsers actually do...
var SAFECHARS = "0123456789" + // Numeric
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
"abcdefghijklmnopqrstuvwxyz" +
"-_.!~*'()"; // RFC2396 Mark characters
var HEX = "0123456789ABCDEF";
var plaintext = text;
var encoded = "";
for (var i = 0; i < plaintext.length; i++ ) {
var ch = plaintext.charAt(i);
if (ch == " ") {
encoded += "+"; // x-www-urlencoded, rather than %20
} else if (SAFECHARS.indexOf(ch) != -1) {
encoded += ch;
} else {
var charCode = ch.charCodeAt(0);
if (charCode > 255) {
alert( "Unicode Character '"
+ ch
+ "' cannot be encoded using standard URL encoding.\n" +
"(URL encoding only supports 8-bit characters.)\n" +
"A space (+) will be substituted." );
encoded += "+";
} else {
encoded += "%";
encoded += HEX.charAt((charCode >> 4) & 0xF);
encoded += HEX.charAt(charCode & 0xF);
}
}
} // for
//document.URLForm.F2.value = encoded;
return encoded;
};