You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: files/en-us/web/api/url/pathname/index.md
+37-1Lines changed: 37 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,10 @@ The **`pathname`** property of the {{domxref("URL")}} interface represents a loc
13
13
HTTPS, HTTP, or other URLs with [hierarchical schemes](https://www.rfc-editor.org/rfc/rfc3986#section-1.2.3) (which the URL standard calls "[special schemes](https://url.spec.whatwg.org/#special-scheme)") always have at least one (invisible) path segment: the empty string.
14
14
The `pathname` value for such URLs will therefore always have at least one `/` character.
15
15
16
-
For non-hierarchical schemes, if the URL has no path segments, the value of its `pathname` property will be the empty string.
16
+
For non-hierarchical schemes, the pathname is known as an _opaque path_ (meaning, the URL parser does not try to split it into a list of segments). In this case, an empty path results in the `pathname` property being the empty string. Trailing spaces in opaque paths are stripped during initial parsing if the `hash` and `search` are both empty; otherwise, they are percent-encoded as `%20` even when `hash` and `search` are later set to empty strings.
17
+
18
+
> [!NOTE]
19
+
> Percent-encoding trailing spaces in opaque paths is not widely implemented. Some browsers implement the old behavior of stripping trailing spaces from `pathname` whenever the `hash` and `search` properties are both empty strings. In these browsers, setting `hash` or `search` may change the `pathname` as well.
When the URL uses a non-hierarchical scheme, the `pathname` property behaves slightly differently. The following example shows a `data:` URL with no path at all, in which case the `pathname` is the empty string.
67
+
68
+
```js
69
+
consturl=newURL("data:");
70
+
console.log(JSON.stringify(url.pathname)); // ""
71
+
```
72
+
73
+
Browsers always strip trailing spaces from `pathname`.
However, if the hash or search are not empty during initial parsing, the trailing space is either preserved (old behavior) or percent-encoded (new behavior).
81
+
82
+
```js
83
+
consturl=newURL("data:text/plain,Hello #frag");
84
+
console.log(JSON.stringify(url.pathname)); // "text/plain,Hello " (old) or "text/plain,Hello%20" (new)
85
+
```
86
+
87
+
If they are later set to empty strings, the trailing space is either removed (old behavior) or remains percent-encoded (new behavior).
88
+
89
+
```js
90
+
consturl=newURL("data:text/plain,Hello #frag");
91
+
url.hash="";
92
+
console.log(JSON.stringify(url.pathname)); // "text/plain,Hello" (old) or "text/plain,Hello%20" (new)
93
+
```
94
+
95
+
Both behaviors ensure that serializing and parsing the URL round-trip; that is, `new URL(url.href).href` is always equal to `url.href`. If the trailing space remains as-is after removing the hash, then `new URL()` would strip it.
0 commit comments