获取当前url的参数


获取当前url的参数

正则

例:当前页面 URL 为:http://test.com?username=admin&password=123456

function getQueryString(name) {
  let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  let r = window.location.search.substr(1).match(reg);
  if (r != null) {
    return decodeURIComponent(r[2]);
  };
  return null;
}

let pwd = getQueryString("password"); // 获取
console.log(pwd); // 123456