Skip to content

parseURL

解析获取 URL 参数

参数

参数名参数类型参数说明
urlstringurl地址

源代码&使用

ts
import { parseURL } from "@manzhixing/utilsxy"

parseURL('http://botaoonline.com.cn/search?a=1&b=2');

{
  a: '1',
  b: '2',
}
ts
/*
 * @Author: Chengbotao
 * @Contact: https://github.com/chengbotao
 */
export function parseURL(url: string) {
  const instanceURL = new URL(url);
  const URLParams = instanceURL.search.slice(1);
  if (!URLParams) return null;
  const splitURLParams = URLParams.split('&');
  const params = Object.create(null);
  splitURLParams.forEach((param) => {
    if (param.includes('=')) {
      const [key, val] = param.split('=');
      params[key] = decodeURIComponent(val);
    } else {
      params[param] = true;
    }
  });
  return params;
}