url 地址处理(截取,参数等)

时间:2022-01-14 17:00:44
function hrefObj() {
  var localhref = window.location.href;
var localarr = localhref.split('?')[1].split('&');
console.log(localarr)
var tempObj = {};
for (var i = 0; i < localarr.length; i++) {
  tempObj[localarr[i].split('=')[0]] = localarr[i].split('=')[1];
    console.log(i)
}
return tempObj;
}

// 截取地址参数
function getCode() {
  var aId = window.location.href.split('?')[1].split('=')[1];
  return aId;
}
截取地址字符串分隔
// var openid = $state.params.openid;
var paramsArr = location.href.match(/\?\S+/)[0].replace('?', '').split('&');
vm.params = {};
for (var i = 0; i < paramsArr.length; i++) {
var tmp = paramsArr[i].split('=');
vm.params[tmp[0]] = tmp[1];
}
var urlStr = window.location.href.split('?')[1].split('&')[0].split('=')[1]
function getUrlParam(key) {
var url = window.location.search.substring(1);
var par = url.split('&');
for (var i = 0; i < par.length; i++) {
var code = par[i].split('=');
if (key == code[0]) return code[1];
}
return "";
}
调用获取code
var code = getUrlParam('code')
 
export function getUrlParams(key) {
let url = window.location.href;
const arr = url.split("?");
const newArr = arr[1] ? arr[1].split("&") : [];
for (var i = 0; i < newArr.length; i++) {
let temp = newArr[i].split("=");
if (temp[0] === key) {
let search = temp[1];
if (search.indexOf("#") > 0) {
search = search.substring(0, search.indexOf("#"));
}
return search;
}
}
}
this.parkId = getUrlParams('parkId')