Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

将URL参数解析成Json对象 #88

Open
bojue opened this issue Apr 23, 2020 · 0 comments
Open

将URL参数解析成Json对象 #88

bojue opened this issue Apr 23, 2020 · 0 comments

Comments

@bojue
Copy link
Owner

bojue commented Apr 23, 2020

  1. 字符串解析的方式
let queryParams = function(_url) {
  let params = {};
  let url = _url || location.search;
  let arr = (url.indexOf('?') > -1 ? url.substr(1) : url).split("&");
  let len = arr.length;
  for(let i=0;i<len;i++) {
    let item = arr[i] && arr[i].split('=');
    params[item[0]] = item[1]
  }
  return params;
}

let res =  queryParams('?name=zhansgan&age=25');
  1. 正则表达式
let queryParams = function(_url) {
  let params = {};
  let url = _url || location.search;
  let arr = (/^[?#]/.test(url) ? url.substr(1) : url).split("&");
  let len = arr.length;
  for(let i=0;i<len;i++) {
    let item = arr[i] && arr[i].split('=');
    params[item[0]] = item[1]
  }
  return params;
}

let res =  queryParams('name=zhansgan&age=215');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant