JS 从URL中截取参数

648 阅读1分钟

根据URL中的参数方法名称,获取到方法值。

eg: http://localhost:5555/1111?username=mubaisama

通过调用 [ getQueryStringChinese (username) ] 返回 [ mubaisama ]

  /**
   * 根据URL中的参数名获取参数值
   * @param name 参数名称
   * @returns {string|null}
   */
  function getQueryStringChinese (name) {
    const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
    const r = window.location.search.substr(1).match(reg)
    if (r != null) return decodeURI(r[2])
    return null
  }