Javascript urldecode function Code _javascript tips

Source: Internet
Author: User
Sometimes there may be such a demand that I use in the background:
Copy Code code as follows:

Httputility.urlencode (str, System.Text.Encoding.UTF8);

The URL is encoded, the front desk JS need to use this content, this time need to decode:

Code
Copy Code code 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);
}
Related Article

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.