http://localhost:80/rprf-piopew?id=100610&type=39&access_token=98c5d5b
针对上述http接口请求 得到自己想要的数据
:
const param = location.href.split('?')[1]
const paramList = param && param.split('&')
let access_token = ''
paramList.forEach(t => {
if (t.indexOf('id') > -1) {
this.previewId = t.split('=')[1]
}
if (t.indexOf('type') > -1) {
this.previewType = t.split('=')[1]
}
if (t.indexOf('access_token') > -1) {
access_token = t.split('=')[1]
}
})
split()方法的使用及要点
1.split()方法主要用于将一个字符串分隔成多个字符串数组
2.split()方法的参数可以是一个或多个每个参数之间用 | 隔开并且每个参数之间都要紧挨着|
3.如果遇到 例如 ?. ( ) | * 等等 需要在它前面加上转义字符\
var str = "I lover your too"
var a = str.split("")
console.log(a);
//不传任何切割标志时,默认切割每一个字符
//返回["I", " ", "l", "o", "v", "e", "r", " ", "y", "o", "u", " ", "t", "o", "o"]
var b = str.split(" ",2)
console.log(b);
//以空格切割返回前两个数组元素
//返回["I", "lover"]
封装函数
function gup(name, url){
if (!url) url = location.href;
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(url);
// console.log(results,'results') ---> ['&type=39', '39', index: 41, input: 'http://localhost:80/rprf-piopew?id=100610&type=39&access_token=98c5d5b', groups: undefined]
return results === null ? null : results[1]
}
// 取url参数,例如:gup('type', 'http://localhost:8080/demo/xxx?type=123');
// gup('access_token', 'http://localhost:80/rprf-piopew?id=100610&type=39&access_token=98c5d5b');
gup('type', 'http://localhost:80/rprf-piopew?id=100610&type=39&access_token=98c5d5b');
gup('id', 'http://localhost:80/rprf-piopew?id=100610&type=39&access_token=98c5d5b');
gup('access_token', 'http://localhost:80/rprf-piopew?id=100610&type=39&access_token=98c5d5b');