JavaScript URL encoding and decoding functions

Source: Internet
Author: User
Tags chr

JavaScript tutorial URL encoding and decoding function
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;
}

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.