JavaScript Get URL Path without last segment

As requested by OP - with changes from comment

var url = document.URL, 
    shortUrl=url.substring(0,url.lastIndexOf("/"));

this assumes the URL is not likely to change

I use document.URL since that is what is recommended

Answer:1

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

Answer:2

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;
    }
}
Answer:3

The transition() selection which i use to shift the position of svg elements when new data comes in, works perfectly when browser window is open but when I open another tab or minimize the window, ...

The transition() selection which i use to shift the position of svg elements when new data comes in, works perfectly when browser window is open but when I open another tab or minimize the window, ...

I need to format my pie and column charts to show the $ and comma in currency format ($###,###) when you hover over the charts. Right now, it is displaying the number and percentage but the number as #...

I need to format my pie and column charts to show the $ and comma in currency format ($###,###) when you hover over the charts. Right now, it is displaying the number and percentage but the number as #...

For my webpage, I chose a font that works well for all the letters. However, for all numbers I'd like to use a different font. Is there a way that I can set a CSS rule to target all the numbers on ...

For my webpage, I chose a font that works well for all the letters. However, for all numbers I'd like to use a different font. Is there a way that I can set a CSS rule to target all the numbers on ...

i have an HTML with 2 ng-includes. Consider if one of the ng-include src is not present in the server. as of now it would just load blank html and in the browser console it would say http-404 file not ...

i have an HTML with 2 ng-includes. Consider if one of the ng-include src is not present in the server. as of now it would just load blank html and in the browser console it would say http-404 file not ...

  1. angular http error handling best practices
  2. angular http error handling interceptor
  3. angular http error handling example
  4. angular http error handler
  5. angular http error message
  6. angular http error catch
  7. angular 6 http error handling
  8. angular 5 http error handling
  9. angular http post error handling
  10. angular 7 http error handling
  11. angular 4 http error handling
  12. angular http request error handling
  13. angular http get error handling
  14. angular 8 http error handling
  15. angular http service error handling
  16. angular httpclient error handling
  17. angular 2 http error handling
  18. angular http observable error handling
  19. angular http response error handling
  20. angular http subscribe error handling