Home > Web Front-end > JS Tutorial > Two methods for jquery to obtain parameters in URL to solve the problem of Chinese garbled characters_javascript skills

Two methods for jquery to obtain parameters in URL to solve the problem of Chinese garbled characters_javascript skills

WBOY
Release: 2016-05-16 17:08:17
Original
1237 people have browsed it

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

Copy code The code is as follows:

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 like this:
Copy code The code is as follows:

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

Method 2:
Copy code The code is as follows:



Call like this:
Copy code The code is as follows:



If the parameter contains Chinese characters, Pay attention to encoding and decoding:
Copy code The code is as follows:

1. Parameter passing page
Javascript code:
2. Receive parameter page: test02.html
<script> <br>var urlinfo = window.location.href;//Get url <br>var userName = urlinfo.split("?")[1] .split("=")[1];//Split the url to get the parameters after "="<br>$("#userName").html(decodeURI(userName)); <br></script>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template