Skip to content

[js] 第12天 写一个获取当前url查询字符串中的参数的方法 #36

Open
@haizhilin2013

Description

@haizhilin2013

第12天 写一个获取当前url查询字符串中的参数的方法

Activity

coderfee

coderfee commented on May 9, 2019

@coderfee
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;
}
qqdnnd

qqdnnd commented on May 20, 2019

@qqdnnd
function urlParam(){
    const param = {};
    location.search.replace(/([^&=?]+)=([^&]+)/g,(m,$1,$2)=> param[$1] = $2);
    return param;
}
tzjoke

tzjoke commented on May 29, 2019

@tzjoke

不考虑IE的话,直接使用url api

Damon99999

Damon99999 commented on Jun 18, 2019

@Damon99999
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 // 可以直接获取,如果结合上面方法可以省一些代码
pma934

pma934 commented on Jun 28, 2019

@pma934
function urlParam(url){
    const param = {};
    url.replace(/[?&](.*?)=([^&]*)/g,(m,$1,$2)=> param[$1] = $2);
    return param;
}
bWhirring

bWhirring commented on Jul 2, 2019

@bWhirring
function getParams(name) {
	const { search }  = window.location;
	const params = new URLSearchParams(search);
	return params.get(name)
}
zzccww

zzccww commented on Jul 4, 2019

@zzccww

window.location.search.match(/(\w)+(?==)/g)

Konata9

Konata9 commented on Jul 11, 2019

@Konata9
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);
shufangyi

shufangyi commented on Jul 18, 2019

@shufangyi
const parsingURLSearch = url => {
  const searchs = url.match(/[^?&=]*=[^?&=]*/g)
  return (
    searchs &&
    searchs.reduce(
      (pre, s) => ({
        ...pre,
        [s.split('=')[0]]: s.split('=')[1]
      }),
      {}
    )
  )
}
Vi-jay

Vi-jay commented on Jul 26, 2019

@Vi-jay
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' }
AchillesV

AchillesV commented on Jul 29, 2019

@AchillesV
const getParmas = (url) => {
  return url.split('&').map((Param) => {
    return Param.split('=')[1]
  })
}
jiamianmao

jiamianmao commented on Aug 8, 2019

@jiamianmao
function getParams(url, key) {
   let oUrl = new URL(url)
  return oUrl.searchParams.get(key)
}
10901182936

10901182936 commented on Aug 26, 2019

@10901182936

@coderfe const定义的常量不能赋值吧

wsypower

wsypower commented on Aug 26, 2019

@wsypower
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) 

41 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @smile-2008@haizhilin2013@zebratt@Konata9@wenxd

        Issue actions

          [js] 第12天 写一个获取当前url查询字符串中的参数的方法 · Issue #36 · haizlin/fe-interview