獲取參數function (get請求方式)

使用前可以先 console.log(window.location) 看看需要的參數在hash、search、href那個裏面,然後選擇使用那個

// 獲取參數
// 使用前可以先 console.log(window.location) 看看需要的參數在hash、search、href那個裏面,然後選擇使用那個
export const getUrlParams = (name) => {
  const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)') // 構造一個含有目標參數的正則表達式對象
  const r = window.location.hash.substr(1).match(reg) // 匹配目標參數 (hash)
  // const r = window.location.search.substr(1).match(reg) //匹配目標參數 (histroy)
  // const r = window.location.href.substr(1).match(reg) //匹配目標參數 (href)
  if (r != null) return unescape(r[2])
  return null // 返回參數值
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章