关于javascript:封装一个方法获取页面url中的参数值

30次阅读

共计 353 个字符,预计需要花费 1 分钟才能阅读完成。

封装一个办法获取页面 url 参数,可作为框架根底办法应用:

// 获取 url 参数; 正则获取 url 参数,蕴含 hash[#] 和 search[?] 两种通用
export function getUrlQueryByName(param) {const reg = new RegExp('(^|&)' + param + '=([^&]*)(&|$)');
  const r =
    window.location.search.substr(1).match(reg) ||
    window.location.hash
      .substring(window.location.hash.search(/\?/) + 1)
      .match(reg);
  if (r != null) {return decodeURIComponent(r[2]);
  }
}

卡布奇诺今犹在,不见当年倒茶人~😌

正文完
 0