new URL(input[, base])


  • input <string> 要解析的绝对或相对的输入网址。 如果 input 是相对的,则需要 base。 如果 input 是绝对的,则忽略 base
  • base <string> | <URL> 如果 input 不是绝对的,则为要解析的基本网址。

通过相对于 base 解析 input 来创建新的 URL 对象。 如果 base 作为字符串传入,则其将被解析为等效于 new URL(base)

const myURL = new URL('/foo', 'https://example.org/');
// https://example.org/foo

网址构造函数可作为全局对象的属性访问。 也可以从内置的 url 模块中导入:

console.log(URL === require('url').URL); // 打印 'true'.

如果 inputbase 不是有效的网址,则将抛出 TypeError。 注意,会将给定的值强制转换为字符串。 例如:

const myURL = new URL({ toString: () => 'https://example.org/' });
// https://example.org/

出现在 input 的主机名中的 Unicode 字符将使用 Punycode 算法自动转换为 ASCII。

const myURL = new URL('https://測試');
// https://xn--g6w251d/

只有在启用 ICU 的情况下编译 node 可执行文件时,此功能才可用。 如果不是,则域名将原封不动地传入。

如果事先不知道 input 是否是绝对的网址并且提供了 base,则建议验证 URL 对象的 origin 是否符合预期。

let myURL = new URL('http://Example.com/', 'https://example.org/');
// http://example.com/

myURL = new URL('https://Example.com/', 'https://example.org/');
// https://example.com/

myURL = new URL('foo://Example.com/', 'https://example.org/');
// foo://Example.com/

myURL = new URL('http:Example.com/', 'https://example.org/');
// http://example.com/

myURL = new URL('https:Example.com/', 'https://example.org/');
// https://example.org/Example.com/

myURL = new URL('foo:Example.com/', 'https://example.org/');
// foo:Example.com/
  • input <string> The absolute or relative input URL to parse. If input is relative, then base is required. If input is absolute, the base is ignored.
  • base <string> | <URL> The base URL to resolve against if the input is not absolute.

Creates a new URL object by parsing the input relative to the base. If base is passed as a string, it will be parsed equivalent to new URL(base).

const myURL = new URL('/foo', 'https://example.org/');
// https://example.org/foo

The URL constructor is accessible as a property on the global object. It can also be imported from the built-in url module:

console.log(URL === require('url').URL); // Prints 'true'.

A TypeError will be thrown if the input or base are not valid URLs. Note that an effort will be made to coerce the given values into strings. For instance:

const myURL = new URL({ toString: () => 'https://example.org/' });
// https://example.org/

Unicode characters appearing within the host name of input will be automatically converted to ASCII using the Punycode algorithm.

const myURL = new URL('https://測試');
// https://xn--g6w251d/

This feature is only available if the node executable was compiled with ICU enabled. If not, the domain names are passed through unchanged.

In cases where it is not known in advance if input is an absolute URL and a base is provided, it is advised to validate that the origin of the URL object is what is expected.

let myURL = new URL('http://Example.com/', 'https://example.org/');
// http://example.com/

myURL = new URL('https://Example.com/', 'https://example.org/');
// https://example.com/

myURL = new URL('foo://Example.com/', 'https://example.org/');
// foo://Example.com/

myURL = new URL('http:Example.com/', 'https://example.org/');
// http://example.com/

myURL = new URL('https:Example.com/', 'https://example.org/');
// https://example.org/Example.com/

myURL = new URL('foo:Example.com/', 'https://example.org/');
// foo:Example.com/