0

how can i get url parameters from string in javascript?instead of using window.location.search. for example let a ="https://google.com/hello?name=roger&age=20" i need to get the all the parameters i found this example but it uses window.location.search

var urlParams;
(window.onpopstate = function () {
    var match,
        pl     = /\+/g,  // Regex for replacing addition symbol with a space
        search = /([^&=]+)=?([^&]*)/g,
        decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
        query  = window.location.search.substring(1);

    urlParams = {};
    while (match = search.exec(query))
       urlParams[decode(match[1])] = decode(match[2]);
})();
biillitil
  • 141
  • 1
  • 12
  • If your browser supports it, you can use `let url = new URL(a); let query = url.search.substring(1)`. See https://developer.mozilla.org/en-US/docs/Web/API/URL/URL – Phil Apr 27 '20 at 04:26
  • _"search"_, not _"serach"_ – Phil Apr 27 '20 at 04:33

0 Answers0