Javascript get first path from url

If I have a url like:

http://localhost:53830/Organisations/1216/View

I want to alert the first part of the url path in lowercase format e.g. 'organisations'

So far I have:

var first = $(location).attr('pathname');

first.indexOf(1);

first.replace('/', '');

first.toLowerCase();

alert(first);

but it's not working as intended. Can anyone help? Thanks

Javascript get first path from url

Samuel Liew

73.6k105 gold badges156 silver badges238 bronze badges

asked Nov 10, 2011 at 15:48

Javascript get first path from url

0

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'
];

answered Nov 10, 2011 at 15:56

Javascript get first path from url

Samuel LiewSamuel Liew

73.6k105 gold badges156 silver badges238 bronze badges

3

var first = $(location).attr('pathname');

first.indexOf(1);

first.toLowerCase();

first = first.split("/")[1];

alert(first);

answered Nov 10, 2011 at 15:50

DogbertDogbert

204k40 gold badges383 silver badges391 bronze badges

0

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

answered Nov 10, 2011 at 15:53

AntagonistAntagonist

5824 silver badges17 bronze badges

You can use URL

const first = new URL(location.href).pathname.split("/")[1]

answered Feb 8 at 14:05

You may need to add window keyword so you don't get error:Cannot read property 'pathname' of undefined

var location = window.location.pathname.split('/')[1]

answered Jan 4, 2020 at 6:07

DAVID AJAYIDAVID AJAYI

1,60019 silver badges13 bronze badges

Not the answer you're looking for? Browse other questions tagged javascript jquery url path or ask your own question.

JavaScript can access the current URL in parts. For this URL:

https://css-tricks.com/example/index.html?s=flexbox
  • window.location.protocol = “http:”
  • window.location.host = “css-tricks.com”
  • window.location.pathname = “/example/index.html”
  • window.location.search = “?s=flexbox”

So to get the full URL path in JavaScript:

var newURL = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname + window.location.search

A bit of a more modern way of working with URLs is the URL() global method.

If you need to break up up the pathname, for example, a URL like https://css-tricks.com/blah/blah/blah/index.html, you can split the string on “/” characters

var pathArray = window.location.pathname.split('/');

Then access the different parts by the parts of the array, like

var secondLevelLocation = pathArray[0];

To put that pathname back together, you can stitch together the array and put the “/”‘s back in:

var newPathname = "";
for (i = 0; i < pathArray.length; i++) {
  newPathname += "/";
  newPathname += pathArray[i];
}

Probably the quickest way to take a peak at what you have is to put window.location in the DevTools console and see:

Javascript get first path from url

How do I find the path of a URL?

The path refers to the exact location of a page, post, file, or other asset. It is often analogous to the underlying file structure of the website. The path resides after the hostname and is separated by “/” (forward slash). The path/file also consists of any asset file extension, such as images (.

How do you separate a URL?

To help our algorithms understand separate mobile URLs, we recommend using the following annotations:.
On the desktop page, add a rel="alternate" tag pointing to the corresponding mobile URL. ... .
On the mobile page, add a rel="canonical" tag pointing to the corresponding desktop URL..

What is URL pathname?

The pathname property of the URL interface is a string containing an initial / followed by the path of the URL, not including the query string or fragment (or the empty string if there is no path).

How do I find the path location of a Windows href?

Window Location.
window.location.href returns the href (URL) of the current page..
window.location.hostname returns the domain name of the web host..
window.location.pathname returns the path and filename of the current page..
window.location.protocol returns the web protocol used (http: or https:).