/*
* params: 无
* return: 返回一个对象,对象中存储url中的数据
*/
~ function() {
//封装获取url参数
function queryUrlParams() {
let obj = {};//定义一个空对象
this.replace(/([^?#&=]+)=([^?#&=]+)/g, (...[, $1, $2]) => { /获取参数
obj[$1] = $2//将参数存储到对象中
});
this.replace(/#([^=&?#]+)/g, (...[, $1]) => { //获取哈希值
obj['HASH'] = $1;//将哈希值存储到对象中
});
return obj;/将对象返回
};
String.prototype['queryUrlParams'] = queryUrlParams; //挂载到原型
}();
//测试
let url1 = 'www.baidu.com/?name=teal&age=29#video';
console.log(url1.queryUrlParams()); /{name: "teal", age: "29", HASH: "video"}
let url2 = 'www.baidu.com/?name=teal&age=29';
console.log(url2.queryUrlParams()); /{name: "teal", age: "29"}
let url3 = 'www.baidu.com/#video';
console.log(url3.queryUrlParams()); /{HASH: "video"}
queryUrlParams
最新推荐文章于 2024-07-10 10:51:41 发布