我正在尝试在网页顶部添加一个“返回目录”按钮,这会将用户重定向到相同的 URL,但其中没有文件名。
例如,在查看 URL 时单击该按钮
http://example.com/somedir/button.html
会将您重定向到
所以我创建了以下代码:
<html>
<body>
<input type="button"
value="back to dir"
onclick="top.location=document.URL.replace(/[^\\]*$/, '')">
</body>
</html>
但我遗漏了正确的代码,它会从 document.URL
请问大家有什么好主意吗?
这是 JSFiddle 链接:http://jsfiddle.net/afarber/PERBY/
而且我不想这一次使用 jQuery。
最佳答案
试试这个 document.URL.substr(0,document.URL.lastIndexOf('/'))
它肯定会起作用!
关于javascript - 如何在 JavaScript 中获取当前 URL 的目录部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16984943/