Home  >  Article  >  Web Front-end  >  Two ways to get url parameter value in js_javascript skills

Two ways to get url parameter value in js_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:23:021349browse
Method 1: Regular Analysis
Copy the 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;
}

Calling method:
alert(GetQueryString("Parameter name 1")); alert(GetQueryString("Parameter name 2"));
alert(GetQueryString("Parameter name 3"));

Method 2
Copy code The code is as follows:

< Script language="javascript">
function GetRequest() {
var url = location.search; //Get the string after the "?" character in the url
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;
}


Calling method:
Statement:
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