下面的代码对我有用
<html>
<body>
<input type="button" value="Click Here" border="2" onClick="test();" >
<script>
function test() {alert(window.location.href);}
</script>
</body>
</html>
结果是“file:///C:/Users/Administrator/Desktop/test.html”有没有办法将结果检索为“file:///C:/Users/Administrator/Desktop/”
谢谢
最佳答案
是的。具有简单的字符串操作方法.substr()
和 .lastIndexOf()
:
var theLocation = "file:///C:/Users/Administrator/Desktop/test.html";
// Get a substring of the original that starts at the beginning of the string
// and ends where the last / is, plus one (to include the / at the end).
var theNewLocation = theLocation.substr(0, theLocation.lastIndexOf("/") + 1);
console.log(theNewLocation);
关于javascript - 调用部分 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50451140/