jquery gets two methods of solving the problem of Chinese garbled in URL parameters

Source: Internet
Author: User
Tags split

  from a page through the URL to the B page, get the URL of the parameters in Chinese garbled problem, the correct way to parse the URL parameters are as follows, interested friends can refer to the following

When you pass a page through a URL to page b, the parse URL parameter can be:  in the following two ways: regular analysis   code as follows: function getquerystring (name) {  var reg = new RegEx P ("(^|&)" + name + "= ([^&]*) (&|$)", "I");  var r = window.location.search.substr (1). Match (REG);  if (r!= null) return unescape (r[2]); Return null; }    This calls:  code as follows: Alert (getquerystring ("parameter name 1"));  alert (getquerystring ("parameter Name 2") ;  alert ("Parameter name 3");    method Two:  code is as follows: <span style= "FONT-SIZE:16PX;" ><script language= "javascript" >  function getrequest () {  var url = location.search;//Get URL "?" Character string   var therequest = new Object ();  if (Url.indexof ("?")!=-1) {  var str = url.substr (1);  STRs = Str.split ("&");  for (var i = 0; i < strs.length i + +) {  therequest[strs[i].split ("=") [0]]=unescape (str S[i].split ("=") [1]); } }  return therequest; }  </Script></span>    This calls the:  code as follows: <SCRIPT language= "javascript" >  var request = new Object ();  request = Getrequest ();  var parameter 1, Parameter 2, parameter 3, parameter n;& nbsp Parameter 1 = request[' parameter 1 '];  parameter 2 = request[' parameter 2 '];  parameter 3 = request[' parameter 3 '];  parameter n = request[' parameter n '];  </scrip t>    If the parameter contains Chinese characters, note the transcoding and decoding:  code as follows: <span style= "FONT-SIZE:18PX;" >1. Parameters page   JavaScript code: <script type= "Text/javascript" >  function Send () {  var url = " Test01.html ";  var userName = $ (" #userName "). html ();  window.open (encodeURI (url +"? username= "+ UserName)); }  </script>  2. Receive Parameters page:test02.html  <script>  var urlinfo = window.location.href;//Get url  var userName = Urlinfo.split ("?") [1].split ("=") [1];//the split URL gets the parameter after "="   $ ("#userName"). HTML (decodeURI (userName));  </script></ span>     

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.