At this time, encodeURIComponent and decodeURIComponent appear, which can fully encode and decode the URL. However, if some transcoding tasks such as those used by the search engine fail to be implemented, it is okay, PHP officially released a solution:
Copy codeThe Code is as follows:
DecodeURIComponent (str + ''). replace (// \ +/g, '% 20 '));
He can decode the address of the search engine perfectly, because the search engine is used to transcoding the space. I forgot to say that I am still used to transcoding Chinese characters...
Then all the above solutions collapsed when they encountered Chinese characters. Yes, I also crashed, that is, part of the code and the Chinese language .. What makes me feel like this...
You can check it for a long time, not to mention what you know...
Copy codeThe Code is as follows:
<Script type = "text/vbscript">
Function str2asc (strstr)
Str2asc = hex (asc (strstr ))
End Function
Function asc2str (ascasc)
Asc2str = chr (ascasc)
End Function
</Script>
<Script type = "text/javascript">
/* UrlEncode and UrlDecode <a href = "/? Tag = % E5 % 87% BD % E6 % 95% B0 "target =" _ blank "> function </a> */
Function UrlEncode (str ){
Var ret = "";
Var strSpecial = "! \ "# $ % & '() * +,/:; <=>? [] ^ '{| }~ % ";
Var tt = "";
For (var I = 0; I <str. length; I ++ ){
Var chr = str. charAt (I );
Var c = str2asc (chr );
Tt + = chr + ":" + c + "n ";
If (parseInt ("0x" + c)> 0x7f ){
Ret + = "%" + c. slice (0, 2) + "%" + c. slice (-2 );
} Else {
If (chr = "")
Ret + = "+ ";
Else if (strSpecial. indexOf (chr )! =-1)
Ret + = "%" + c. toString (16 );
Else
Ret + = chr;
}
}
Return ret;
}
Function UrlDecode (str ){
Var ret = "";
For (var I = 0; I <str. length; I ++ ){
Var chr = str. charAt (I );
If (chr = "+ "){
Ret + = "";
} Else if (chr = "% "){
Var asc = str. substring (I + 1, I + 3 );
If (parseInt ("0x" + asc)> 0x7f ){
Ret + = asc2str (parseInt ("0x" + asc + str. substring (I + 4, I + 6 )));
I + = 5;
} Else {
Ret + = asc2str (parseInt ("0x" + asc ));
I + = 2;
}
} Else {
Ret + = chr;
}
}
Return ret;
}
</Script>
This is true only for browsers that support VB... For example, FF does not support...
Why does VB need to be used? This is because str2asc and asc2str cannot meet JS requirements... I have simulated two JS methods below, which does not seem to be effective for all Chinese characters ..
Copy codeThe Code is as follows:
Function str2asc (str ){
Return str. charCodeAt (0). toString (16 );
}
Function asc2str (str ){
Return String. fromCharCode (str );
}