方法一:正则
const GetQueryString = (name) => { let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); let r = window.location.search.substr(1).match(reg); if (r != null) return decodeURIComponent(r[2]); return null; }; GetQueryString(name)
方法二:
function getQueryString(parameterName) { const path = window.location.href; const parameterIndex = path.indexOf(parameterName); if (parameterIndex !== -1) { const valueStartIndex = parameterIndex + parameterName.length + 1; const valueEndIndex = path.indexOf('&', valueStartIndex) !== -1 ? path.indexOf('&', valueStartIndex) : path.length; const value = path.substring(valueStartIndex, valueEndIndex); return value } } getQueryString("name")
方法三:
const getUrlParam = (url, paraName) => { let arrObj = url.split("?"); if (arrObj.length > 1) { let arrPara = arrObj[1].split("&") let arr; for (let i in arrPara) { arr = arrPara[i].split("=") if (arr != null && arr[0] == paraName) { return arr[1] } } return "" } else { return "" } } getUrlParam(url,"name")
标签:arr,return,name,路径,js,let,path,const,地址栏 From: https://www.cnblogs.com/huxiuxiu/p/17548996.html