<script language="JavaScript">
function UrlEncode(str)
{
var i,c,p,q,ret="",strSpecial="!\"#$%&'()*+,/:;<=>?@[\]^`{|}~%";
for(i=0;i<str.length;i++){
if(str.charCodeAt(i)>=0x4e00){
var p=strGB.indexOf(str.charAt(i));
if(p>=0){
q=p%94;
p=(p-q)/94;
ret+=("%"+(0xB0+p).toString(16)+"%"+(0xA1+q).toString(16)).toUpperCase();
}
}
else{
c=str.charAt(i);
if(c==" ")
ret+="+";
else if(strSpecial.indexOf(c)!=-1)
ret+="%"+str.charCodeAt(i).toString(16);
else
ret+=c;
}
}
return ret;
}
</script>

解决方案 »

  1.   

    多谢hozi!:):那么解码函数又是如何的呢?还请帮助!!!再次感谢
      

  2.   

    decodeComponent和encodeComponent
    这两个函数就搞定了
      

  3.   

    你可以直接用.encodeURL,decodeURI<script>
    document.write(encodeURI("http://www.csdn.net/index.asp?t=灰豆"));
    document.write("<br>")
    document.write(decodeURI(encodeURI("http://www.csdn.net/index.asp?t=灰豆")));
    </script>
      

  4.   

    <script language="VBScript">
    function URLDecode(enStr)
    dim deStr,strSpecial
    dim c,i,v
      deStr=""
      strSpecial="!""#$%&'()*+,/:;<=>?@[\]^`{|}~%"
      for i=1 to len(enStr)
        c=Mid(enStr,i,1)
        if c="%" then
          v=eval("&h"+Mid(enStr,i+1,2))
          if inStr(strSpecial,chr(v))>0 then
            deStr=deStr&chr(v)
            i=i+2
          else
            v=eval("&h"+Mid(enStr,i+1,2)+Mid(enStr,i+4,2))
            deStr=deStr&chr(v)
            i=i+5
          end if
        else
          if c="+" then
            deStr=deStr&" "
          else
            deStr=deStr&c
          end if
        end if
      next
      URLDecode=deStr
    end function
    </script>