15

How can I get the URL Path of the current site, but without the last segment:

http://www.domain.com/first/second/last

I only need http://www.domain.com/first/second … with jQuery (or only JavaScript)

3
  • 2
    var firstpart = url.substring(0,url.lastIndexOf("/")) Commented Dec 12, 2012 at 17:42
  • 1
    @mplungjan: please add your Comment as a Answer, this one works! Commented Dec 12, 2012 at 18:01
  • 1
    Possible duplicate of Last segment of URL Commented Oct 4, 2015 at 15:58

4 Answers 4

21

Using pop and URL api

this assumes the URL is not likely to change

I use document.URL since that is what is recommended

const url = new URL("https://www.example.com/first/second/last"); // new URL(document.URL)
let path = url.pathname.split("/");
path.pop(); // remove the last
url.pathname = path.join("/")
console.log(url)

Older answers: As requested by OP - with changes from comment

const url = "http://www.example.com/first/second/last", // document.URL, 
    shortUrl=url.substring(0,url.lastIndexOf("/"));
console.log(shortUrl)    

Here is an alternative

const url = new URL("http://www.example.com/first/second/last"),
      shortUrl = `${url.protocol}//${url.hostname}${url.pathname.slice(0,url.pathname.lastIndexOf("/"))}`

console.log(shortUrl)

Sign up to request clarification or add additional context in comments.

2 Comments

does work with: var url = document.location.href, shortURL = url.substring(0,url.lastIndexOf("/"));
but document.location.href is deprecated for document.URL, hence I use that
8

http://jsfiddle.net/KZsEW

Try the following for all browsers:

var url = "http://www.domain.com/first/second/last";  // or var url = document.URL;
var subUrl = url.substring(0,url.lastIndexOf("/"))

alert(subUrl);
​

The lastIndexOf() method returns the position of the last occurrence of a specified value in a string.

Note: The string is searched from the end to the beginning, but returns the index starting at the beginning, at postion 0.

This method returns -1 if the value to search for never occurs.

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/lastIndexOf

3 Comments

You're using lastIndexOf on a string, not on an Array, so there is no need to modify the array prototype.
Sorry! I deleted to stop any more confusion, I was confused myself.
Really neat answer as it's essentially a one-liner that can be used in array.map()
0

Try this:

var url = 'http://www.domain.com/first/second/last';
for(var i=url.length-1; i>=0;i--){
    if(url[i]!='/'){
        url= url.substr(0,i);
    }
    else{
        alert(url);
        break;
    }
}

Comments

0

I'm not sure this is the most elegant of solutions, but you just want the substring up to the last slash, or second to last if the last character is a slash. Here I first take the part of the URL that appears after the protocol (http:// or https://) so that on for example http://stackoverflow.com it returns http://stackoverflow.com.

    var url = document.URL.split('://');
    var last_slash;
    var result;
    if (url[1].charAt(url[1].length - 1) === '/') {
      url[1] = url[1].substring(0, url[1].length - 1);
    }
    last_slash = url[1].lastIndexOf('/');
    result = url[0] + '://' + ((last_slash !== -1) ? url[1].substring(0, last_slash) : url[1]);

edit: jsfiddle http://jsfiddle.net/CV6d4/

5 Comments

You're already using split, so why not split the string on '/' instead of '://', pop the last item from the array and join them again. jsfiddle.net/8chxf
You'd have to tweak it to get the corner cases, i.e. if you add a "/" at the end of the URL in your solution it doesn't work, and it also doesn't work on "domain.com".
document.location is deprecated for document.URL - window.location.href is also valid.
Alright, fair enough. A more robust solution might worth the extra code.
Thanks @mplungjan, didn't know that document.location was deprecated -- good to know!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.