URL:pathname 属性

URL:pathname 属性

带不透明路径的 pathname

当 URL 使用非分层方案时,pathname 属性的行为略有不同。以下示例显示了一个完全没有路径的 data: URL,在这种情况下,pathname 为空字符串。

jsconst url = new URL("data:");

console.log(JSON.stringify(url.pathname)); // ""

在初始解析期间,如果不存在 hash 或 search,浏览器总是会从 pathname 中删除尾部空格。

jsconst url = new URL("data:text/plain,Hello ");

console.log(JSON.stringify(url.pathname)); // "text/plain,Hello"

但是,如果在初始解析期间 hash 或 search 不为空,则尾部空格要么被保留(旧行为),要么被百分比编码(新行为)。

jsconst url = new URL("data:text/plain,Hello #frag");

console.log(JSON.stringify(url.pathname)); // "text/plain,Hello " (old) or "text/plain,Hello%20" (new)

如果它们稍后被设置为空字符串,尾部空格要么被删除(旧行为),要么保持百分比编码(新行为)。

jsconst url = new URL("data:text/plain,Hello #frag");

url.hash = "";

console.log(JSON.stringify(url.pathname)); // "text/plain,Hello" (old) or "text/plain,Hello%20" (new)

这两种行为都确保了 URL 的序列化和解析能够进行往返;也就是说,new URL(url.href).href 始终等于 url.href。如果移除 hash 后尾部空格保持不变,那么 new URL() 会将其删除。

相关推荐

银行卡正常工资突然打不进去什么原因?银行卡工资不能到账的原因是什么?
《旅行青蛙》吃饭要吃多久 青蛙一直吃饭怎么解决
365体育亚洲官方入口app下载

《旅行青蛙》吃饭要吃多久 青蛙一直吃饭怎么解决

📅 12-17 👁️ 6788
《新版三国》关羽斩颜良、诛文丑是第几集?