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)

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
albuvee
  • 2,744
  • 6
  • 28
  • 38

4 Answers4

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)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
7

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

KingKongFrog
  • 13,946
  • 21
  • 75
  • 124
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/

Emily
  • 5,869
  • 1
  • 22
  • 15
  • 1
    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. http://jsfiddle.net/8chxf/ – nxt Dec 12 '12 at 18:04
  • 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 "http://www.domain.com". – Emily Dec 12 '12 at 18:08
  • document.location is deprecated for document.URL - window.location.href is also valid. – mplungjan Dec 12 '12 at 18:16
  • Alright, fair enough. A more robust solution might worth the extra code. – nxt Dec 12 '12 at 18:17
  • Thanks @mplungjan, didn't know that document.location was deprecated -- good to know! – Emily Dec 12 '12 at 18:26
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;
    }
}
A. Magalhães
  • 1,511
  • 2
  • 22
  • 31