Get the first part of a url path
var first = $(location).attr('pathname');
first.indexOf(1);
first.toLowerCase();
first = first.split("/")[1];
alert(first);
try to use first.split('/')
so you will end up with an array of strings like
['http:' ,'', 'localhost:53830' , 'Organisations' , '1216' , 'View' ]
then find the one the is right after localhost:53830
This will never throw an error because pathname always starts with a /
, so the minimum length of the resulting array will be two after splitting:
const firstPath = location.pathname.split('/')[1];
If we are at the domain root, the returned value will be an empty string ''
const path = location.pathname.split('/');
path = [
'',
'questions',
'8082239',
'get-the-first-part-of-a-url-path',
'8082346'
];
You can use URL
const first = new URL(location.href).pathname.split("/")[1]