We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Learn more about funding links in repositories.
Report abuse
There was an error while loading. Please reload this page.
第12天 写一个获取当前url查询字符串中的参数的方法
function params() { const search = window.location.search; search = search.substr(1, search.length); const res = {}; if (!search) return res; search.split('&').map(item => { const [key, value] = item.split('='); res[key] = decodeURIComponent(value); }); return res; }
function urlParam(){ const param = {}; location.search.replace(/([^&=?]+)=([^&]+)/g,(m,$1,$2)=> param[$1] = $2); return param; }
不考虑IE的话,直接使用url api
url = "https://github.com/kk?tab=stars&assetId=311&page=DETAIL&projectPhase=2"; function splitUrl(url) { if(typeof url !== "string") return; var obj = {}; url.split("?")[1].split("&").forEach(item => { var arr = [key, value] = item.split("=") obj[arr[0]] = arr[1]; }) console.log(obj); } splitUrl(url);
window.location.search // 可以直接获取,如果结合上面方法可以省一些代码
function urlParam(url){ const param = {}; url.replace(/[?&](.*?)=([^&]*)/g,(m,$1,$2)=> param[$1] = $2); return param; }
function getParams(name) { const { search } = window.location; const params = new URLSearchParams(search); return params.get(name) }
window.location.search.match(/(\w)+(?==)/g)
const getLocationParams = () => { const paramsMap = new Map(); if (location.search) { // 第一个字符串为 ? location.search .substr(1) .split("&") .forEach((param) => { const [key, value] = param.split("="); paramsMap.set(key, value); }); } return paramsMap; }; const getLocationParam = (key) => getLocationParams().get(key);
const parsingURLSearch = url => { const searchs = url.match(/[^?&=]*=[^?&=]*/g) return ( searchs && searchs.reduce( (pre, s) => ({ ...pre, [s.split('=')[0]]: s.split('=')[1] }), {} ) ) }
const url = "www.baidu.com?asd=asd%asd&dsa=2"; function getParamsByUrl(url) { const obj = {}; url.replace(/([^?&]+)=([^?&]+)/g, (__, c1, c2) => { obj[c1] = c2; }); return obj } console.log(getParamsByUrl(url));//{ asd: 'asd%asd', dsa: '2' }
const getParmas = (url) => { return url.split('&').map((Param) => { return Param.split('=')[1] }) }
function getParams(url, key) { let oUrl = new URL(url) return oUrl.searchParams.get(key) }
@coderfe const定义的常量不能赋值吧
const getURLParameters = url => (url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce( (a, v) => ( (a[v.slice(0, v.indexOf("="))] = v.slice(v.indexOf("=") + 1)), a ),{} ); getURLParameters(window.location.href)
Activity
coderfee commentedon May 9, 2019
qqdnnd commentedon May 20, 2019
tzjoke commentedon May 29, 2019
不考虑IE的话,直接使用url api
Damon99999 commentedon Jun 18, 2019
pma934 commentedon Jun 28, 2019
bWhirring commentedon Jul 2, 2019
zzccww commentedon Jul 4, 2019
window.location.search.match(/(\w)+(?==)/g)
Konata9 commentedon Jul 11, 2019
shufangyi commentedon Jul 18, 2019
Vi-jay commentedon Jul 26, 2019
AchillesV commentedon Jul 29, 2019
jiamianmao commentedon Aug 8, 2019
10901182936 commentedon Aug 26, 2019
@coderfe const定义的常量不能赋值吧
wsypower commentedon Aug 26, 2019
41 remaining items