url中各个参数问题

// https://www.bilibili.com/video/BV1fZ4y1Q7t3?spm_id_from=333.851.b_7265636f6d6d656e64.3#123

location.protocol // https
location.host // www.bilibili.com
location.pathname // /video/BV1fZ4y1Q7t3
location.search // ?spm_id_from=333.851.b_7265636f6d6d656e64.3
location.hash // #123
location.href // https://www.bilibili.com/video/BV1fZ4y1Q7t3?spm_id_from=333.851.b_7265636f6d6d656e64.3#123

两种方法获取URL中的查询参数

function serilizeUrl(url) {
  var result = {};
  url = url.split("?")[1];
  var map = url.split("&");
  for(var i = 0, len = map.length; i < len; i++) {
      result[map[i].split("=")[0]] = map[i].split("=")[1];
  }
  return result;
}
function serilizeUrl (url) {
  url = new URL(url)
  const urlSearch = new URLSearchParams(url.search)
  const result = {}
  for (const p of urlSearch) {
    result[p[0]] = p[1] ? p[1] : null
  }
  return result
}

results matching ""

    No results matching ""