js 把url参数转对象
//注意url中要含?
function getParameterByName(name, url) {
if (!url) {
url = window.location.href;
}
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
调用:getParameterByName("userID
","?userID=JeoOrCXxyiOFxbYaGL40kw==&userPwd=sdFo2ziUw8HyLRKd4i6GAQ==&userName=高聪
");
得到:userID
值
var parseQuery = function (query) {
var reg = /([^=&\s]+)[=\s]*([^&\s]*)/g;
var obj = {};
while (reg.exec(query)) {
obj[RegExp.$1] = RegExp.$2;
}
return obj;
}
调用:
parseQuery("userID=JeoOrCXxyiOFxbYaGL40kw==&userPwd=sdFo2ziUw8HyLRKd4i6GAQ==&userName=高聪
")
得到
![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA+MAAACTCAIAAABqLF/bAAAOKElEQVR4nO3dPU7rTBsGYC8oEkJiL6lcZSMpaFKwjDRUaVhDGspIn8QmUiIhYL7CfzP2OPy8h5MJ57pEARN7xvELem8/eexTBQAAoDzVuQ8AAADIkNQBAKBE1f8AAIDyqKkDAECJJHUAACiRpA4AACWS1AEAoESSOgAAlEhSBwCAEknqAABQIkkdAABKJKkDwF+3q8PVJhw2oarPfSicm18G5l1KUn+6u6k6N3dP8Y/d2LkPEQA+ZxeqOoRDuLoKh3MfC71NfY7/HH4ZOOVSknoITVpvE3n0bfejtA7AZajrEHK5sK5C1X1dbb446SFcdfvuPjyAaKFdCLs6VFVb0G1eqj+cYuYANofxSHxI7ULR12YmnO7qUE2Sax2dmc3VR2cpWr15O+3qM4H4sBm/1J6lbrDZ/Rsn5rS5XwYIIfxsUh/F6YdVVa0e+m877VA01u3zsKpu7p6a8dXDqaTe799NBgAX6RCuvtUCUfepd/dBE0UdBfHNVbvXrh52/05M//Qh1XEa3p2K2nUuUg+Dh1CfvJiJV+9X2VzNRu3NJllxczXk+373+geS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