HOME> 世界杯比利时> Web APIs URL.pathname 简体中文

Web APIs URL.pathname 简体中文

URL:路径名属性

基线 广泛可用

此功能已非常完善,适用于多种设备和浏览器版本。自 2015 年 7 月起,该功能已在各种浏览器中可用。

Learn more

查看完整兼容性

Report feedback

注意:此功能在 Web Workers 中可用。

URL 接口的 pathname 属性表示层次结构中的位置。它是由路径段列表构成的字符串,每个路径段都以 / 字符为前缀。

HTTPS、HTTP 或其他带有 hierarchical schemes (URL 标准称之为 " special schemes " )的 URL 始终至少有一个(不可见的)路径段:空字符串。因此,此类 URL 的 pathname 值始终至少有一个 / 字符。

对于非分层方案,如果 URL 没有路径段,其 pathname 属性的值将为空字符串。

Value

A string.

Examples

具有不可见段的路径名

下面的 URL 只有一个路径段,即空字符串。 pathname 值是通过在空字符串前面添加 / 字符构成的。

const url = new URL("https://developer.mozilla.org");

console.log(url.pathname); // 日志 "/"

带有查询参数的路径名

下面的示例显示了带有查询参数的 HTTPS URL 的路径名。

const url = new URL(

"https://developer.mozilla.org/en-US/docs/Web/API/URL/pathname?q=value",

);

console.log(url.pathname); // 日志 "/en-US/docs/Web/API/URL/pathname"

查询参数不构成路径的一部分。请注意,某些系统使用 ; 和 = 字符来分隔适用于路径段的参数和参数值。例如,对于 URL https://example.org/users;id=42/tasks;state=open?sort=modified ,系统可能会从路径段 users;id=42 和 tasks;state=open 中提取并使用路径段参数 id=42 和 state=open 。

带有 slug 的路径名

某些系统将术语 slug 定义为非空路径的最后一段,如果它用人类可读的关键字来标识页面的话。例如,下面的 URL 有 slug this-that-other-outre-collection 。

const url = new URL(

"https://example.org/articles/this-that-other-outre-collection",

);

console.log(url.pathname); // 日志 "/articles/this-that-other-outre-collection"

Specifications

Specification

URL # dom-url-路径名

Browser compatibility

Desktop

Mobile

Chrome

Edge

Firefox

Opera

Safari

Chrome Android

Android 版 Firefox

Opera Android

IOS 上的 Safari

Samsung Internet

WebView Android

pathname

32

13

22在 Firefox 53 之前, pathname 和 search 为自定义协议返回了错误值。假设 protocol:host/x?a=true&b=false , pathname 将返回 "/x?a=true&b=false" ,而 search 将分别返回 "", rather than " /x " and " ?a=true&b=false"。请参阅 bug 1310483 。

19

10

32

22在 Firefox for Android 53 之前, pathname 和 search 为自定义协议返回了错误值。假设 protocol:host/x?a=true&b=false , pathname 将返回 "/x?a=true&b=false" ,而 search 将分别返回 "", rather than " /x " and " ?a=true&b=false"。请参阅 bug 1310483 。

19

10

2.0

4.4.3

See also

它所属的 URL 接口。

© 2005–2024 MDN 贡献者。根据 Creative Commons Attribution-ShareAlike 许可证 v2.5 或更高版本获得许可。

https://developer.mozilla.org/en-US/docs/Web/API/URL/pathname

友情链接