Home >Web Front-end >JS Tutorial >Get parameters in URL using JavaScript
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");
Method 2: Regular method
unction request(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; } // 这样调用: alert(request("mid"));