获取两个绝对URL之间的相对URL

问题描述 投票:0回答:1

给定两个绝对 URL:

const from = new URL("https://example.com")
const to = new URL("https://example.com/about")

有没有一种简单可靠的方法来获取它们之间的相对网址? (在这种情况下

about

需要处理:

  • 来源不同
  • 非子页面(例如
    https://example.com/some-page
    https://example.com/some-other-page
  • 查询参数和片段

理想情况下,当用作锚点上的

href
属性时,任何方法都会返回从 URL a 导航到 URL b 的最短 URL。

javascript url routes navigation
1个回答
0
投票

我最终实现了这个函数,它实现了我想要的功能。

© www.soinside.com 2019 - 2024. All rights reserved.