Using location.search to locate a parameter's value

前端 未结 4 1621
走了就别回头了
走了就别回头了 2021-02-09 16:05

I’m working on a tool which takes the value parameters in the URL and does a few things with them.

My issue is, I can’t seem to use document.location to show the specifi

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-09 16:43

    let url = new URL('www.examplesite.com?yourname=gilgilad');
    let searchParams = new URLSearchParams(url.search);
    console.log(searchParams.get('yourname'));
    

    you can consider also to user window.location or window.location.search directly

    let searchParams = new URLSearchParams(window.location.search);
    console.log(searchParams.get('yourname'));
    

提交回复
热议问题