对于绝对 URL,我们可以使用 new URL(str)
进行解析;对于基于基础 URL 的相对 URL,我们可以使用 new URL(path, base)
。如果没有基础 URL,那么该如何解析相对 URL 呢?例如:folder/file.ext?a=1&b=2#hash
应被解析为:
{
pathname: "folder/file.ext",
search: "?a=1&b=2",
hash: "#hash"
}
第三方库没问题,但我更喜欢内置的库和函数。偏好跨平台(浏览器/Node.js)解决方案。不需要考虑IE浏览器。
www.example.com/
并解析它... - vakunew URL('folder/file.ext?a=1&b=2#hash', 'http://dummy');
作为解决方法。 - Zlatko