获取URL中的参数

方法一:

let url = new URLSearchParams('www.baidu.com?post=123&action=edit');

console.log(url.get('action')); // 打印出edit

 

方法二:

利用window.location.search

getUrl (param) { 

  let query = window.location.search.substring(1);

  let urls = query.split('&');

  for (let i=0; i < urls.length; i++) {

    let url = urls[i].split('=');

    if (url[0] === param) { return url[1]}

  }

  return(false);

}

eg: www.baidu.com?a=1&b=2

let a = this.getUrl('a');

console.log(a); // 打印a的值为1

posted @ 2019-10-09 10:24  hugge  阅读(239)  评论(0)    收藏  举报