First by being specified in the URL parameter is acquired
/ * * * Get the URL specified parameters * URL: HTTP:? //Www.xxx.com/index name = 123 * Parameters: param URL Parameters * call the method: getParam ( "name") * Return value: 123 * Alert (getParam ( 'DATE')); * / function getParam (name) { var REG = new new the RegExp ( "(^ | &)" + name + "= ([^ &] *) (& | $)", "i" ); // Search, a query? The latter parameters, and matches the regular var R & lt location.search.substr = (. 1 ) .match (REG); IF (R & lt =! Null ) return decodeURI (decodeURI (R & lt [2 ])); }
The second can either get all the parameters in the URL, you can also get a single parameter
/ * * * JS url parameter value acquisition
* ideas, the value following the question mark url acquired by the search location, a question mark string was filtered off, and then divided by a split method parameter set, the assignment cycle, to match the corresponding parameters, and finally return value * ? the URL of: HTTP: //www.xxx.com/index name = 123 * parameters: param URL parameters * call the method: getUrlParams ( "name") * return value: 123 * Alert (getUrlParams ( 'DATE')); * / function getUrlParams (name) { // do not pass all the return value name, otherwise the corresponding value var URL = the window.location.search; IF ( '?' url.indexOf () ==. 1) { return to false ;} URL = url.substr (. 1 ); URL = url.split ( '&' ); var name = name || '' ; var nameres; // get all parameters and their values for ( var I = 0; I <url.length; I ++ ) { var info URL = [I] .split ( '=' ); var obj = {}; obj [ info [ 0]] = decodeURI (info [. 1 ]); URL [I] = obj; } // If you pass a parameter name, matches its value IF (name) { for ( var I = 0; I <URL .length; I ++ ) { for (const Key in URL [I]) { IF (Key == name) { nameres = URL [I] [Key]; } } } } the else { nameres = URL; } // return result return nameres; }
const url = 'http://www.abc.com/test.php?id=1&from=index'; var res = getUrlParams(); var res1 = getUrlParams('id'); console.log(res); // [{id: "1"}, {from: "index"}] console.log(res1); // 1
The third Gets a collection of parameter names and parameter values in the URL
/ * * * [Get set parameter names and parameter values in the URL] * Example URL: http:? //HtmlJsTest/getrequest.html uid = admin & rid = 1 & fid = 2 & name = Bob * @param {[string]} urlStr [ when this parameter is not empty, then the resolution in the parameter set url] * @return {[String]} [parameters set] * / function GetRequest (urlStr) { iF ( typeof urlStr == "undefined" ) { var decodeURI = url (location.search); // Get the url string symbol "?" } the else { var url = + urlStr.split [. 1 "?" ( "?") ]; } var theRequest = new 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]] = decodeURI(strs[i].split("=")[1]); } } return theRequest; }
var parms_1 = GetRequest(); 2 console.log(parms_1); // {"uid":"admin","rid":"1","fid":"2","name":"小明"} 3 console.log(parms_1['name']); // '小明' 4 var parms_2 = GetRequest('http://htmlJsTest/getrequest.html?uid=admin&rid=1&fid=2&name=小明'); 5 console.log(parms_2); // {"uid":"admin","rid":"1","fid":"2","name":"小明"} 6 console.log(parms_2['name']); // '小明'
The fourth acquisition url parameter values in the parameter name
/ * * * [Obtain the parameter values in the url parameter Name] * Example URL: http:? //HtmlJsTest/getrequest.html uid = admin & rid = 1 & fid = 2 & name = Bob * @param {[string]} queryName [ Parameter Name ] * @return {[String]} [parameters] * / function GetQueryValue (The queryName is) { var Query = decodeURI (window.location.search.substring (. 1 )); var VARS = query.split ( "&" ); for ( var I = 0; I <vars.length; I ++ ) { var pair VARS = [I] .split ( "=" ); IF (pair [0] == The queryName is) { return pair [. 1 ];} } return null; }
1 var queryVal=GetQueryValue('name'); 2 console.log(queryVal);// 小明