Window location.pathname

实例

获取当前路径:

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <h2>pathname 属性</h2>
  5. <p id="demo">
  6. <script>
  7. let path = location.pathname;
  8. document.getElementById("demo").innerHTML = path;
  9. </script>
  10. </body>
  11. </html>

定义与用法

pathname 属性设置或返回 URL(页面)的路径名。


语法

返回 pathname 属性:

  1. location.pathname

设置 pathname 属性:

  1. location.pathname = path

Property Values

参数描述
pathURL 的路径名

返回值

类型描述
A string当前路径名。

浏览器支持

location.pathname 所有浏览器都支持:

属性
location.pathnameYesYesYesYesYes

分类导航