-1

How can we get part of string added to the url using java script.

example: my url link is in below format:

https://domain/imp/s/testrecordname/idoftestrecord/selectedrecord?languagelocale

i tried below ways.

var v1=window.location.href
var v2=window.location.host
var v3=window.location.hostname
var v4=window.location.protocol
var v5=window.location.pathname
var v6=window.location.search
var v7=window.location.hash

but i am unable to get "testrecordname/idoftestrecord/selectedrecord" this portion of url.

can anyone suggest how to attain it

dileep
  • 7
  • 3
  • `location.pathname` gets you `/imp/s/testrecordname/idoftestrecord/selectedrecord`, if you only need parts of that, you will have to get there yourself using some string manipulation. – CBroe Mar 06 '20 at 12:51
  • Does this answer your question? [How do I parse a URL into hostname and path in javascript?](https://stackoverflow.com/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript) – icecub Mar 06 '20 at 12:55
  • location.pathname gets only "/imp/s" . Till here only its fetching – dileep Mar 06 '20 at 12:57

2 Answers2

0

You need to manipulate the pathname

const url = new URL("https://domain/imp/s/testrecordname/idoftestrecord/selectedrecord?languagelocale")
console.log(url.href);
console.log(url.host);
console.log(url.hostname);
console.log(url.protocol);

console.log(url.pathname); // this???

console.log(url.pathname.split("/s/")[1]); // for example


console.log(url.search);
console.log(url.hash);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

One way could be extracting from URL string using string functions

let s = 'https://domain/imp/s/testrecordname/idoftestrecord/selectedrecord?languagelocale';
console.log(s.slice(s.indexOf('s/')+2,s.indexOf('?')));
Mridul
  • 1,320
  • 1
  • 5
  • 19