A lot of friends say JavaScript decodeURI function can also be implemented, but there are bugs all of it, below look at the following function, tested using the temporary no problem, I said in the previous article, this and PHP UrlDecode function is not the same thing. Here is my JavaScript version of the UrlDecode function based on the code of High Man, the reference link in the beginning of the article mentioned, not one of the list. As with the previous UrlEncode function, only the UTF-8 version is implemented.
1, UrlEncode
Use method: UrlEncode (str);
function UrlEncode (clearstring) {var output = ';
var x = 0;
clearstring = Utf16to8 (clearstring.tostring ()); var regex =/(^[a-za-z0-9-_.]
*)/;
while (x < clearstring.length) {var match = Regex.exec (Clearstring.substr (x));
if (match!= null && match.length > 1 && match[1]!= ') {output + = match[1];
x + + match[1].length;
else {if (clearstring[x] = = ') output = = ' + ';
else {var charcode = clearstring.charcodeat (x);
var hexval = charcode.tostring (16); Output + = '% ' + (Hexval.length < 2?)
' 0 ': ') + hexval.touppercase ();
} x + +;
} function Utf16to8 (str) {var out, I, Len, C;
out = "";
len = str.length;
for (i = 0; i < len; i++) {c = str.charcodeat (i);
if ((c >= 0x0001) && (c <= 0x007f)) {out = = Str.charat (i); else if (C > 0x07ff) {out + = String.fromCharCode (0xe0 |
((c >>) & 0x0f)); Out + = String.fromchArcode (0x80 |
((c >> 6) & 0x3F)); Out + + string.fromcharcode (0x80 |
((c >> 0) & 0x3F)); else {out = = String.fromCharCode (0xc0 |
((c >> 6) & 0x1F)); Out + + string.fromcharcode (0x80 |
((c >> 0) & 0x3F));
} return out;
return output; }
2, UrlDecode
How to use: UrlDecode (URL);
function UrlDecode (encodedstring) {var output = encodedstring;
var binval, thisstring;
var myregexp =/(%[^%]{2})/;
function Utf8to16 (str) {var out, I, Len, C;
var char2, Char3;
out = "";
len = str.length;
i = 0;
while (I < len) {c = str.charcodeat (i++);
Switch (c >> 4) {case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:out + = Str.charat (i-1);
Break
Case 12:case 13:char2 = str.charcodeat (i++); Out + + String.fromCharCode ((C & 0x1F) << 6) |
(Char2 & 0x3F));
Break
Case 14:char2 = str.charcodeat (i++);
CHAR3 = Str.charcodeat (i++);
Out + + String.fromCharCode ((C & 0x0f) << 12) |
((Char2 & 0x3F) << 6) |
((Char3 & 0x3F) << 0));
Break
} return out; while (match = myregexp.exec (output))!= null && match.length > 1 && match[1]!= ') {bi
Nval = parseint (match[1].substr (1), 16); Thisstring = String.fromCharCode (Binval);
Output = Output.replace (match[1], thisstring);
}//output = utf8to16 (output);
Output = Output.replace (/\\+/g, "");
Output = utf8to16 (output);
return output; }
When the server-side through PHP urlencode transcoding can be used to resolve the JS UrlDecode.