【问题标题】:Get URL Path without last segment获取没有最后一段的 URL 路径
【发布时间】:2012-11-30 11:53:06
【问题描述】:

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

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

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

【问题讨论】:

  • var firstpart = url.substring(0,url.lastIndexOf("/"))
  • @mplungjan:请添加您的评论作为答案,这个有效!
  • Last segment of URL的可能重复

标签: javascript jquery url


【解决方案1】:

使用 pop 和 URL api

假设 URL 不太可能改变

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

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)

较早的答案:根据 OP 的要求 - 对评论进行更改

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

这是另一种选择

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)

【讨论】:

  • 适用于:var url = document.location.href, shortURL = url.substring(0,url.lastIndexOf("/"));
  • 但 document.location.href 已被 document.URL 弃用,因此我使用它
【解决方案2】:

http://jsfiddle.net/KZsEW

为所有浏览器尝试以下操作:

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

alert(subUrl);
​

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

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

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

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

【讨论】:

  • 你是在字符串上使用lastIndexOf,而不是在数组上,所以不需要修改数组原型。
  • 对不起!我删除了以防止更多混乱,我自己也很困惑。
【解决方案3】:

试试这个:

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

【讨论】:

    【解决方案4】:

    我不确定这是最优雅的解决方案,但您只希望子字符串到最后一个斜杠,或者如果最后一个字符是斜杠,则为倒数第二个。在这里,我首先获取出现在协议(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]);
    

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

    【讨论】:

    • 你已经在使用 split,所以为什么不在 '/' 而不是 '://' 上拆分字符串,从数组中弹出最后一项并再次加入它们。 jsfiddle.net/8chxf
    • 您必须对其进行调整以获取极端情况,即如果您在解决方案中的 URL 末尾添加“/”它不起作用,它也不起作用处理“domain.com”。
    • document.location 对于 document.URL 已弃用 - window.location.href 也有效。
    • 好吧,够公平的。一个更强大的解决方案可能值得额外的代码。
    • 感谢 @mplungjan,不知道 document.location 已被弃用——很高兴知道!
    猜你喜欢
    • 2020-04-13
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    相关资源
    最近更新 更多