1

There is an address of the type site.com/userlist/?getlist=XXXX&userid=XX. I need to get part of the URL, namely /userlist/?getlist=XXXX&userid=XX and insert the button with the ID reg_button.

I'm leaning towards jquery and apparently regex, but I haven't found a suitable method yet or I don't understand it, I'm just learning)

yereler207
  • 11
  • 1

3 Answers3

0

let url_test=new URL('http://www.yourtestdomain.com/userlist/?getlist=XXXX&userid=XX');
console.log(url_test.pathname+url_test.search);
Asutosh
  • 1,775
  • 2
  • 15
  • 22
0

This takes the url minus base (domain) aswell as the query part, and also URN if exists

documentation

console.log(window.location.href.replace(window.location.origin, ""))
Tomm
  • 1,021
  • 2
  • 14
  • 34
0

There are many ways to do it. I have included 2 ways here. URL class can also be used as mentioned by one of the answerer but the URL string needs to be complete with http prefix.

If you want it using regex you check this Stackoverflow thread.

var siteurl = "site.com/userlist/?getlist=XXXX&userid=XX";
function method1(){
  var paths = siteurl.split('/');
  paths.shift();
  var basePath = paths.join('/');
  alert(basePath);
} 

function method2(){
  var basePath = siteurl.substring(siteurl.indexOf('/'));
  alert(basePath);
}
<button onclick = 'method1()'>Method1</button>
<button onclick = 'method2()'>Method2</button>
vbrin27
  • 723
  • 1
  • 6
  • 25