Getting the url parameters inside the html page

后端 未结 4 955
旧时难觅i
旧时难觅i 2020-12-30 07:16

I have a html page which is loaded using a url that looks a little like this:

http://localhost:8080/GisProject/MainService?s=C&o=1

I wo

4条回答
  •  执念已碎
    2020-12-30 07:34

    Assuming that our URL is https://example.com/?product=shirt&color=blue&newuser&size=m, we can grab the query string using window.location.search:

     const queryString = window.location.search;
     console.log(queryString);
     // ?product=shirt&color=blue&newuser&size=m
    

    We can then parse the query string’s parameters using URLSearchParams:

     const urlParams = new URLSearchParams(queryString);
    

    Then we call any of its methods on the result.

    For example, URLSearchParams.get() will return the first value associated with the given search parameter:

     const product = urlParams.get('product')
     console.log(product);
     // shirt
    
     const color = urlParams.get('color')
     console.log(color);
     // blue
    
     const newUser = urlParams.get('newuser')
     console log(newUser);
     // empty string
    

    Other Useful Methods

提交回复
热议问题