javascript 获取没有最后一段的 URL 路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13845486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:56:22  来源:igfitidea点击:

Get URL Path without last segment

javascriptjqueryurl

提问by albuvee

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

如何获取当前站点的 URL 路径,但没有最后一段:

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

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

我只需要http://www.domain.com/first/second……?使用 jQuery(或只需要 JavaScript)

回答by mplungjan

As requested by OP - with changes from comment

根据 OP 的要求 - 评论有所变化

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

this assumes the URL is not likely to change

这假设 URL 不太可能改变

I use document.URLsince that is what is recommended

我使用document.URL因为这是推荐的

回答by KingKongFrog

http://jsfiddle.net/KZsEW

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.

lastIndexOf()方法返回指定值在字符串中最后一次出现的位置。

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.

注意:字符串从尾到头搜索,但返回从头开始的索引,位置 0。

如果从未出现要搜索的值,则此方法返回 -1。

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

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

回答by Emily

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.comit returns http://stackoverflow.com.

我不确定这是最优雅的解决方案,但您只需要子字符串直到最后一个斜杠,或者如果最后一个字符是斜杠,则倒数第二个。在这里,我首先获取出现在协议(http:// 或 https://)之后的 URL 部分,例如在http://stackoverflow.com 上它返回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/

编辑:jsfiddle http://jsfiddle.net/CV6d4/

回答by A. Magalh?es

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;
    }
}