Javascript UrlDecode Function Code

Source: Internet
Author: User

Sometimes there may be such a requirement that I use in the background:
Copy codeThe Code is as follows:
HttpUtility. UrlEncode (str, System. Text. Encoding. UTF8 );

Encode the Url. The front-end JS needs to use this content. In this case, it needs to be decoded:

Code
Copy codeThe Code is as follows:
/**
* Url Encoding
**/
ShengUtils. encode = function (unzipStr ){
Var zipstr = "";
Var strSpecial = "! \ "# $ % & '() * +,/:; <=>? [] ^ '{| }~ % ";
Var tt = "";
For (var I = 0; I <unzipStr. length; I ++ ){
Var chr = unzipStr. charAt (I );
Var c = ShengUtils. StringToAscii (chr );
Tt + = chr + ":" + c + "n ";
If (parseInt ("0x" + c)> 0x7f ){
Zipstr + = encodeURI (unzipStr. substr (I, 1 ));
} Else {
If (chr = "")
Zipstr + = "+ ";
Else if (strSpecial. indexOf (chr )! =-1)
Zipstr + = "%" + c. toString (16 );
Else
Zipstr + = chr;
}
}
Return zipstr;
}

/**
* Url Decoding
**/
ShengUtils. decode = function (zipStr ){
Var uzipStr = "";
For (var I = 0; I <zipStr. length; I ++ ){
Var chr = zipStr. charAt (I );
If (chr = "+ "){
UzipStr + = "";
} Else if (chr = "% "){
Var asc = zipStr. substring (I + 1, I + 3 );
If (parseInt ("0x" + asc)> 0x7f ){
UzipStr + = decodeURI ("%" + asc. toString () + zipStr. substring (I + 3, I + 9). toString ());;
I + = 8;
} Else {
UzipStr + = ShengUtils. AsciiToString (parseInt ("0x" + asc ));
I + = 2;
}
} Else {
UzipStr + = chr;
}
}
Return uzipStr;
}

ShengUtils. StringToAscii = function (str ){
Return str. charCodeAt (0). toString (16 );
}

ShengUtils. AsciiToString = function (asccode ){
Return String. fromCharCode (asccode );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.