用javascript如何把 %D6%D0%B9%FA 解码为 中国只要javascript

解决方案 »

  1.   

    %D6%D0%B9%FA 不是 encodeURI 的结果!L@_@K
    document.write(encodeURI("中国")); // %E4%B8%AD%E5%9B%BD 
      

  2.   

    js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent
      

  3.   

    浏览器在post数据时.会对表单内容根据当前页面的编码进行不同的encodeURI(),或许是用别的函数.中国会被编码成%D6%D0%B9%FA                                     GBK GB2312 GB18030
    %E4%B8%AD%E5%9B%BD                      UTF-8
    %92%86%8D%91                                      Big-5等
    %C3%E6%B9%F1                                       euc-jp等等等,可以自己写个网页试验一下.<form action="http://www.baidu.com/s"><input type="text" name="wd"><input type="submit" value="search"></form>这些编码在服务器端肯定可以通过对应的函数解码.但我想知道用javascript能不能实现解码.
      

  4.   

    不好意思,确实如此,据说 querystring 是 gbk 编码,至于如何解码俺在查查哈
      

  5.   


    <% @ page import="java.net.URLDecoder">
    <%
       String str1;
       str1=URLdecoder.decode(request.getParameter("str"),"UTF-8");
                            //str为传递的数据,编码时用的是encodeURIComponent
    %>
      

  6.   

    解决了,在 btbtd 的网站上找到了,通过编码映射表解决的!
    作者应该是 [email protected],btbtd 整理收集了这个代码!L@_@K
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>shawl.qiu template</title>
    </head>
    <body><script type="text/javascript" src="http://www.btbtd.org/test/sqJsTools/tools/Url/Url/CP936.js
    "></script>
    <script type="text/javascript">
    /*<![CDATA[*/
    /*
    作者:邱少伟
    CSND 账号:http://hi.csdn.net/btbtd
    作者网站:http://www.btbtd.org/
     */
     var pe = "%D6%D0%B9%FA";
     document.write("<p/>pe: "+pe);
     document.write("<p/>de: "+IDecodeURIGb(pe, [CP936]));
     document.write("<hr/>");
     function IDecodeURIGb(Source, CP936) // Pe = percent encoding; %xx[,%xx]
     { // shawl.qiu code, return string|void return; Func: fGetUtfHex
      var bStr = false;
      if(Source.constructor==String) Source = [Source], bStr = true;  //if(CP936==null) fFatalError("编码映射表不能为空!");  var iLBound = iUBound = 0;
      iLBound = -1; iUBound = 0x7f;  Source[0] = Source[0].replace(/\+/g, " ");  var Len = Source[0].length;  for(var i=0; i<Len; i++)
      {
       var iIndex = Source[0].indexOf("%", i);
       if(iIndex===-1) break;   var sHex = Source[0].slice(iIndex+1, iIndex+3);
       var iCode = parseInt(sHex, 16);   //document.write("<br/>sHex: "+sHex);   if(iCode>-1&&iCode<=0x7f) // ascii
       {
        Source[0] =
        [
         Source[0].slice(0, iIndex)
         , String.fromCharCode(iCode)
         , Source[0].slice(iIndex+3)
        ].join("");
       }
       else
       {
        var sHexRaw = Source[0].slice(iIndex+3, iIndex+6);
        //document.write("<br/>sHexRaw: "+sHexRaw);
        if(sHexRaw.charAt(0)!="%") continue;
        sHex += sHexRaw.slice(1);
        var sUniHex = fGetUtfHex(sHex, CP936);
        //document.write("<br/>sHex: "+sHex);
        //document.write("<br/>sUniHex: "+sUniHex);
        Source[0] =
        [
         Source[0].slice(0, iIndex)
         , String.fromCharCode(parseInt(sUniHex, 16))
         , Source[0].slice(iIndex+6)
        ].join("");
       } // end if(iCode<-1&&iCode<0x7f)
      } // end for(var i=0; i<Len; i++)  if(bStr) return Source[0];
     } // end function IDecodeURIGb(Source) function fGetUtfHex(sGbHex, CP936)
     { // shawl.qiu code, return string
      sGbHex = sGbHex.toUpperCase();
      var iIndex = CP936[0].indexOf(sGbHex);
      if(iIndex===-1) return "0x005F";  var iBegin = iIndex+sGbHex.length+1;
      return CP936[0].substring(iBegin, iBegin+6);
     } // end function fGetMapTableChar(sHex)
    /*]]*/
    </script>
    </body>
    </html>