diff --git a/doc/api/url.md b/doc/api/url.md index e1e6c0133ee27e..97cc284a6d500c 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -93,7 +93,9 @@ return `true`. #### Constructor: new URL(input[, base]) -* `input` {string} The input URL to parse +* `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. @@ -125,6 +127,32 @@ const myURL = new URL('https://你好你好'); 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. + +```js +const { URL } = require('url'); + +let myURL = new URL('http://anotherExample.org/', 'https://example.org/'); +// http://anotherexample.org/ + +myURL = new URL('https://anotherExample.org/', 'https://example.org/'); +// https://anotherexample.org/ + +myURL = new URL('foo://anotherExample.org/', 'https://example.org/'); +// foo://anotherExample.org/ + +myURL = new URL('http:anotherExample.org/', 'https://example.org/'); +// http://anotherexample.org/ + +myURL = new URL('https:anotherExample.org/', 'https://example.org/'); +// https://example.org/anotherExample.org/ + +myURL = new URL('foo:anotherExample.org/', 'https://example.org/'); +// foo:anotherExample.org/ +``` + #### url.hash * {string}