JS获取URL的查询参数
(1)将参数转化成JSON对象
(2)将参数存放到map当中
function test2() {
var url = "myapp://index.html?page=index& id = 123";
//var url = "myapp://index.html";
console.log("getUrlParamsToJSON(url):", getUrlParamsToJSON(url));
console.log("");
var params = getUrlParamsToMap(url);
console.log("getUrlParamsToMap(url):", params);
//在map当中,如果key不存在,则返回undefined
console.log("page:", params.get('page'));
console.log("test:", params.get('test'));
}
// 获取URL的查询参数
// 将参数转化成JSON对象:如果URL没有携带参数,则JSON对象为{}
function getUrlParamsToJSON(url) {
var params = {};
//去除所有空格
url = url.replace(/\s/ig, '');
//正则表达式匹配
url.replace(/([^?&=]+)