Home > Web Front-end > JS Tutorial > body text

Get parameters in URL using JavaScript

高洛峰
Release: 2016-12-06 16:05:25
Original
1325 people have browsed it

This article will share with you two methods to use js to obtain the parameters in the url. The second method is the regular expression method. You can choose a better method according to your needs. Without further ado, let’s go straight to the detailed introduction.

Method 1:

//取url参数 var type = request("type")
function request() {
var query = location.search;
var paras = arguments[0];
if (arguments.length == 2) {
query = arguments[1];
}
if (query != "") {
if (query.indexOf("?") != -1) {
query = query.split("?")[1];
}
query = query.split("&");
for (var i = 0; i < query.length; i++) {
var querycoll = query[i].split("=");
if (querycoll.length == 2) {
if (querycoll[0].toUpperCase() == paras.toUpperCase()) {
return querycoll[1];
break;
}
}
}
}
return "";
}
//调用方法
var mid=request("mid");
Copy after login

Method 2: Regular method

unction request(name) {
var reg = new RegExp(&#39;(^|&)&#39; + name + &#39;=([^&]*)(&|$)&#39;, &#39;i&#39;);
var r = window.location.search.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
return null;
}
// 这样调用:
alert(request("mid"));
Copy after login


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