doc: clarify url doc

Indicate that `base` is ignored if `input` is absolute.

PR-URL: https://github.com/nodejs/node/pull/19899
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
James M Snell 2018-04-09 10:59:15 -07:00 committed by Luigi Pinca
parent 345e3b28c6
commit cf48d1db66

View File

@ -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}