如何在JavaScript中获取当前URL的目录部分?
我正在尝试在网页顶部添加一个返回目录按钮,该按钮将重定向到同一个URL,但没有文件名。
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
http://example.com/somedir/button.html
将重定向到
所以我创建了以下代码:
So I've created the following code:
<html>
<body>
<input type="button"
value="back to dir"
onclick="top.location=document.URL.replace(/[^\\]*$/, '')">
</body>
</html>
但是我错过了正确的代码,它会从 document.URL
but I'm missing the correct code, which would shave away the filename from the current URL in document.URL
有人有个好主意吗?
这是JSFiddle链接: http://jsfiddle.net/afarber/PERBY/
Here is the JSFiddle link: http://jsfiddle.net/afarber/PERBY/
我不想一次使用jQuery。
And I'd prefer not to use jQuery this one time.
尝试这个 document.URL.substr(0,document.URL.lastIndexOf('/'))
它将工作肯定!