js从 URL 获取查询参数

传递 window.location 或原始 URL值 goole.com?search=easy&page=3 从 url 轻松检索查询参数

 

const getParameters = (URL) => {
  URL = JSON.parse('{"' + decodeURI(URL.split("?")[1]).replace(/"/g, '\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}');
  return JSON.stringify(URL);
};

getParameters(window.location) // Result: { search : "easy", page : 3 }
posted @ 2021-11-01 10:00  Dz&Ying  阅读(70)  评论(0)    收藏  举报