如何在 JavaScript 中获取当前 URL 的目录部分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16984943/
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
How to get the directory part of current URL in JavaScript?
提问by Alexander Farber
I'm trying to add a "back to dir" button at the top of a web page, which would redirect the user to the same URL, but with no filename in it.
我正在尝试在网页顶部添加一个“返回目录”按钮,该按钮会将用户重定向到相同的 URL,但其中没有文件名。
For example, clicking that button while viewing the URL
例如,在查看 URL 时单击该按钮
http://example.com/somedir/button.html
http://example.com/somedir/button.html
would redirect you to the
会将您重定向到
So I've created the following code:
所以我创建了以下代码:
<html>
<body>
<input type="button"
value="back to dir"
onclick="top.location=document.URL.replace(/[^\]*$/, '')">
</body>
</html>
but I'm missing the correct code, which would shave away the filename from the current URL in document.URL
但我缺少正确的代码,这会从当前 URL 中删除文件名 document.URL
Does anybody have a good idea here please?
请问这里有人有什么好主意吗?
Here is the JSFiddle link: http://jsfiddle.net/afarber/PERBY/
这是 JSFiddle 链接:http: //jsfiddle.net/afarber/PERBY/
And I'd prefer not to use jQuery this one time.
我不想这一次不使用 jQuery。
回答by Abdul Rauf
Try this document.URL.substr(0,document.URL.lastIndexOf('/'))
试试这个 document.URL.substr(0,document.URL.lastIndexOf('/'))
It will work for sure!
它肯定会起作用!
回答by jbyrd
This one-liner also works:
这种单线也有效:
document.URL.split('/').slice(0, -1).join('/')
回答by Brett Zamir
The following gets directories, including the case where the last character is a slash.
下面获取目录,包括最后一个字符是斜杠的情况。
document.URL.replace(/\/[^/]+\/?$/, '');
This effectively states the following (assuming they can be found in this order)
这有效地说明了以下内容(假设可以按此顺序找到它们)
\/
: Find a "/"[^/]+
: Find one or more characters after it that are not "/"\/?
: Find an optional "/" after those character(s)$
: Find the end of the string
\/
: 发现 ”/”[^/]+
: 在它后面找到一个或多个不是“/”的字符\/?
: 在这些字符后找到一个可选的“/”$
: 查找字符串的结尾
Then remove these by ''
, so we're left with the directory only.
然后通过 删除这些''
,所以我们只剩下目录。
Again, this assumes there isa slash present marking a directory and that this is not just a URL where the only slashes are within http://
.
再次,这假定有是一个斜线现在标志着一个目录,这是不是只是其中唯一的斜线都内的URL http://
。
回答by robby dwi hartanto
console.log(new URL(document.URL));
you would get all object like this :
你会得到这样的所有对象:
{
href:"https://yy.xx.id/index.php/5351/user/private/images%20%282%29.jpeg?forcedownload=1",
origin:"https://smi.kerjaindonesia.id",
protocol: "https:",
username: "",
password: "",
pathname: "index.php/5351/user/private/images%20%282%29.jpeg" ,
port: "" ,
protocol: "https:",
search: "?forcedownload=1"
}
回答by Hilson Shrestha
/* to avoid query parameters, use pathname instead of href */
var l = document.location.pathname.split('/');
l.pop();
console.log(document.location.origin + l.join('/'));
回答by Derek Henderson
Try the following, which takes into account both when the URL ends in a trailing slash and when it doesn't:
尝试以下操作,其中考虑了 URL 何时以斜杠结尾和何时不结尾:
var currUrl = document.URL,
newUrl;
if (currUrl.charAt(currUrl.length - 1) === '/') {
newUrl = currUrl.slice(0, currUrl.lastIndexOf('/'));
newUrl = newUrl.slice(0, newUrl.lastIndexOf('/')) + '/';
} else {
newUrl = currUrl.slice(0, currUrl.lastIndexOf('/')) + '/';
}
console.log(newUrl);