Skip to content


静态html用js取中文参数,无乱码

 <coolcode>

<script type=”text/javascript”>  
  String.prototype.getQueryString   =   function(name)  
  {  
      var   reg   =   new   RegExp(“(^|&|\\?)”+   name   +”=([^&]*)(&|$)”),   r;  
      if   (r=this.match(reg))   return   unescape(r[2]);   return   null;  
  };  
  </script>
     <script>
     var msg=window.location.href.getQueryString(“msg”);
     if (msg)
     {
     document.write(‘<li>· ’+msg+'</li>’);
     }
     </script>
</coolcode>

在ff下中文会乱码,参数传递过来时需先escape();

<coolcode>

function phpescape($str){
    preg_match_all(“/[\x80-\xff].|[\x01-\x7f]+/”,$str,$newstr);
    $ar = $newstr[0];
    foreach($ar as $k=>$v){
        if(ord($ar[$k])>=127){
            $tmpString=bin2hex(iconv(“GBK”,”ucs-2″,$v));
            if (!eregi(“WIN”,PHP_OS)){
                $tmpString = substr($tmpString,2,2).substr($tmpString,0,2);
            }
            $reString.=”%u”.$tmpString;
        } else {
            $reString.= rawurlencode($v);
        }
    }
    return $reString;
}

</coolcode>

 

参考:

PHP实现Javascript的escape(),unescape()的方法

Firefox自动对url中的中文进行编码导致乱码

Posted in JavaScript/DOM/XML.

Tagged with , , , , .


One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. 我要DVD says

    呵呵,好久没来这逛下了!希望博主一切顺利!



Some HTML is OK

or, reply to this post via trackback.