在WEB(我是在Asp.net环境,相信其它也一样.)开发当中,当你传有中文值的URL(如http://localhost/Test/test.aspx?name=张三),你会发现你通过Request.Questring["Name"]服务器处理时获取的并不是你要的"张三",而是一些奇怪的字符,问题就来了。这时候你可用通过Javascript的window.encodeURIComponent方法转换你的URL,将中文部分转换成encode。贴入javascript实现的方法.

 function qs(url)
 { 
   
if (window.RegExp && window.encodeURIComponent)
    {
      
return encodeURIComponent(url); 
    } 
 } 
 
你可以参照GOOGLE的方法:
function qs(el) 
{
  
if (window.RegExp && window.encodeURIComponent)
  {
     
var ue=el.href;var qe=encodeURIComponent(document.gs.q.value);
     
if(ue.indexOf("q=")!=-1)
    {
      el.href
=ue.replace(new RegExp("q=[^&$]*"),"q="+qe);
     }
     
else
    {
      el.href
=ue+"&q="+qe;
    }
  }
  
return 1;
}
posted on 2009-07-19 21:42  钱途无梁  阅读(546)  评论(0编辑  收藏  举报