URL字符串获取目录

URL String Get Directory

本文关键字:获取 字符串 URL      更新时间:2023-09-26

我有一个字符串,其中包含一个URL:

var url = "http://www.domain.com/Shared%20Documents/Test%20Plans/CC-12%20Test%20Plan.doc";

我正在设法获取目录的url(即删除文件名并保留其余部分)我意识到这可能很简单,但我似乎找不到任何东西

期望结果:

var url = "http://www.domain.com/Shared%20Documents/Test%20Plans/";
 var resultUrl = url.substring(0, url.lastIndexOf("/") + 1);

在这里工作jsfiddle

您可以使用以下表达式:

location.protocol + '//' + document.domain + location.pathname

地址栏中显示当前URL。

使用子字符串如下。。。。

url=url.substring(0,url.lastindexof("/")-1);