Jquery to get parameters in the URL to solve the problem of Chinese garbled code two ways


When passing parameters from page A to page B through url, the following two methods can be used to resolve url parameters: Method 1: regular analysis


function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}

Call:


alert(GetQueryString(" Parameter names 1"));
alert(GetQueryString(" Parameter names 2"));
alert(GetQueryString(" Parameter names 3"));

Method 2:


<span style="font-size: 16px;"><Script language="javascript">
function GetRequest() {
var url = location.search; //Get the "?" in the url String after a 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(strs[i].split("=")[1]);
}
}
return theRequest;
}
</Script></span>

Call:


<Script language="javascript">
var Request = new Object();
Request = GetRequest();
var  parameter 1, parameter 2, parameter 3, parameter N;
 parameter 1 = Request[' parameter 1'];
 parameter 2 = Request[' parameter 2'];
 parameter 3 = Request[' parameter 3'];
 parameter N = Request[' parameter N'];
</Script>

If the parameter contains Chinese characters, pay attention to transcoding and decoding:


<span style="font-size:18px;">1. The page refs
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;//Access to the url
var userName = urlinfo.split( " ? " )[1].split( " = " )[1];//Split the url to get the argument after "="
$( " #userName " ).html(decodeURI(userName));
</script></span>