在JavaScript中,如果你想显示当前页面的路径,可以使用window.location
对象。以下是一些常用的属性来获取路径信息:
window.location.pathname
: 获取当前页面的路径名,即URL中除去协议、主机名、端口和查询字符串的部分。// 获取当前路径
var currentPath = window.location.pathname;
// 显示当前路径
console.log("当前路径是: " + currentPath);
window.location
是一个包含当前URL信息的对象。pathname
属性返回URL的路径部分,例如对于URL http://example.com/page.html?query=123
,pathname
的值将是 /page.html
。window.location.href
。window.location.hostname
。window.location.search
。window.onload
事件中执行或使用 DOMContentLoaded
事件。window.onload
事件中执行或使用 DOMContentLoaded
事件。通过以上方法,你可以轻松地在JavaScript中获取并显示当前页面的路径。
领取专属 10元无门槛券
手把手带您无忧上云