javascript - 调用部分 URL

标签 javascript html

下面的代码对我有用

<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/

相关文章:

javascript范围隐私破坏?

javascript - 如何在页面加载时防止 css 关键帧动画?

javascript - 样式化新 Chrome 的警告窗口

javascript - 下拉菜单动态填充第二个下拉菜单

php - htmlpurifier 允许特定标签的方案

javascript - 如何使用 Angular2 和 TypeScript 在单击后禁用暂停按钮并在弹出窗口显示后立即启用

javascript - js 全局变量在缩小时被删除

javascript - 附加表行时不复制按钮

css - 如何在一个div中创建多个列

html - 三明治菜单与指针填充相比的问题