-1

I have this url

https://myapp.tezze-now.com/sp?id=form&table=user&sys_id=cb6688db0d79b5e9619&view=sp

I need to get text of table name from the url when page loads. I tried this

location.href.split('&')[1]

It returns "table=user". but I need the table name alone as user.

How can I get this?

krish
  • 1,077
  • 6
  • 24
  • 49

3 Answers3

3

Try using URL.searchParams

 // location.href
let str = "https://myapp.tezze-now.com/sp?id=form&table=user&sys_id=cb6688db0d79b5e9619&view=sp"; 

console.log((new URL(str)).searchParams.get("table"));
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
1

Split it once more:

location.href.split('&')[1].split('=')[1]

Or you could use Url class (no support in IE):

var url = "https://myapp.tezze-now.com/sp?id=form&table=user&sys_id=cb6688db0d79b5e9619&view=sp"; //window.location.href
var url = new URL(url);
var table = url.searchParams.get("table");
console.log(table);
Justinas
  • 41,402
  • 5
  • 66
  • 96
0
location.href.split('table=')[1].split('&')[0]

Don't assume the parameter order ;)

Davesoft
  • 724
  • 1
  • 4
  • 10