第一种
function getQueryParam(key) {
if (!key) {
return false;
}
var value = '';
var paramStr = window.location.search ? window.location.search.substr(1) : '';
if (paramStr) {
paramStr.split('&').forEach(function (param) {
var arr = param.split('=');
if (arr[0] == key) {
value = arr[1];
}
});
}
return value;
}
var url='localhost:8080?name=test&age=20';
getQueryParam(name) // test
第二种
const getQueryParam = (key) => {
const reg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)', 'i');
const r = window.location.search.substr(1).match(reg);
console.log(r)
if (r != null) {
return decodeURI(r[2]);
}
return null;
}