• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v14.19.1">
7  <title>URL | Node.js v14.19.1 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/url.html">
12</head>
13<body class="alt apidoc" id="api-section-url">
14  <div id="content" class="clearfix">
15    <div id="column2" class="interior">
16      <div id="intro" class="interior">
17        <a href="/" title="Go back to the home page">
18          Node.js
19        </a>
20      </div>
21      <ul>
22<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
23<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
24</ul>
25<hr class="line">
26<ul>
27<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
28<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
29<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
30<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
31<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
32<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
33<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
34<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
35<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
36<li><a href="console.html" class="nav-console">Console</a></li>
37<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
38<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
39<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
40<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
41<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
42<li><a href="dns.html" class="nav-dns">DNS</a></li>
43<li><a href="domain.html" class="nav-domain">Domain</a></li>
44<li><a href="errors.html" class="nav-errors">Errors</a></li>
45<li><a href="events.html" class="nav-events">Events</a></li>
46<li><a href="fs.html" class="nav-fs">File system</a></li>
47<li><a href="globals.html" class="nav-globals">Globals</a></li>
48<li><a href="http.html" class="nav-http">HTTP</a></li>
49<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
50<li><a href="https.html" class="nav-https">HTTPS</a></li>
51<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
52<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
53<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
54<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
55<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li>
56<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
57<li><a href="net.html" class="nav-net">Net</a></li>
58<li><a href="os.html" class="nav-os">OS</a></li>
59<li><a href="path.html" class="nav-path">Path</a></li>
60<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
61<li><a href="policy.html" class="nav-policy">Policies</a></li>
62<li><a href="process.html" class="nav-process">Process</a></li>
63<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
64<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
65<li><a href="readline.html" class="nav-readline">Readline</a></li>
66<li><a href="repl.html" class="nav-repl">REPL</a></li>
67<li><a href="report.html" class="nav-report">Report</a></li>
68<li><a href="stream.html" class="nav-stream">Stream</a></li>
69<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
70<li><a href="timers.html" class="nav-timers">Timers</a></li>
71<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
72<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
73<li><a href="tty.html" class="nav-tty">TTY</a></li>
74<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
75<li><a href="url.html" class="nav-url active">URL</a></li>
76<li><a href="util.html" class="nav-util">Utilities</a></li>
77<li><a href="v8.html" class="nav-v8">V8</a></li>
78<li><a href="vm.html" class="nav-vm">VM</a></li>
79<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
80<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
81<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
82</ul>
83<hr class="line">
84<ul>
85<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
86</ul>
87    </div>
88
89    <div id="column1" data-id="url" class="interior">
90      <header>
91        <div class="header-container">
92          <h1>Node.js v14.19.1 documentation</h1>
93          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
94            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
95              <path fill="none" d="M0 0h24v24H0z" />
96              <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
97              <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
98            </svg>
99            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
100              <path d="M0 0h24v24H0z" fill="none" />
101              <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
102            </svg>
103          </button>
104        </div>
105        <div id="gtoc">
106          <ul>
107            <li>
108              <a href="index.html">Index</a>
109            </li>
110            <li>
111              <a href="all.html">View on single page</a>
112            </li>
113            <li>
114              <a href="url.json">View as JSON</a>
115            </li>
116
117    <li class="version-picker">
118      <a href="#">View another version <span>&#x25bc;</span></a>
119      <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v17.x/api/url.html">17.x</a></li>
120<li><a href="https://nodejs.org/docs/latest-v16.x/api/url.html">16.x <b>LTS</b></a></li>
121<li><a href="https://nodejs.org/docs/latest-v15.x/api/url.html">15.x</a></li>
122<li><a href="https://nodejs.org/docs/latest-v14.x/api/url.html">14.x <b>LTS</b></a></li>
123<li><a href="https://nodejs.org/docs/latest-v13.x/api/url.html">13.x</a></li>
124<li><a href="https://nodejs.org/docs/latest-v12.x/api/url.html">12.x <b>LTS</b></a></li>
125<li><a href="https://nodejs.org/docs/latest-v11.x/api/url.html">11.x</a></li>
126<li><a href="https://nodejs.org/docs/latest-v10.x/api/url.html">10.x</a></li>
127<li><a href="https://nodejs.org/docs/latest-v9.x/api/url.html">9.x</a></li>
128<li><a href="https://nodejs.org/docs/latest-v8.x/api/url.html">8.x</a></li>
129<li><a href="https://nodejs.org/docs/latest-v7.x/api/url.html">7.x</a></li>
130<li><a href="https://nodejs.org/docs/latest-v6.x/api/url.html">6.x</a></li>
131<li><a href="https://nodejs.org/docs/latest-v5.x/api/url.html">5.x</a></li>
132<li><a href="https://nodejs.org/docs/latest-v4.x/api/url.html">4.x</a></li>
133<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/url.html">0.12.x</a></li>
134<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/url.html">0.10.x</a></li></ol>
135    </li>
136
137            <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/url.md">Edit on GitHub</a></li>
138          </ul>
139        </div>
140        <hr>
141      </header>
142
143      <details id="toc" open><summary>Table of contents</summary><ul>
144<li><span class="stability_2"><a href="#url_url">URL</a></span>
145<ul>
146<li><a href="#url_url_strings_and_url_objects">URL strings and URL objects</a>
147<ul>
148<li><a href="#url_constructing_a_url_from_component_parts_and_getting_the_constructed_string">Constructing a URL from component parts and getting the constructed string</a></li>
149</ul>
150</li>
151<li><a href="#url_the_whatwg_url_api">The WHATWG URL API</a>
152<ul>
153<li><a href="#url_class_url">Class: <code>URL</code></a>
154<ul>
155<li><a href="#url_new_url_input_base"><code>new URL(input[, base])</code></a></li>
156<li><a href="#url_url_hash"><code>url.hash</code></a></li>
157<li><a href="#url_url_host"><code>url.host</code></a></li>
158<li><a href="#url_url_hostname"><code>url.hostname</code></a></li>
159<li><a href="#url_url_href"><code>url.href</code></a></li>
160<li><a href="#url_url_origin"><code>url.origin</code></a></li>
161<li><a href="#url_url_password"><code>url.password</code></a></li>
162<li><a href="#url_url_pathname"><code>url.pathname</code></a></li>
163<li><a href="#url_url_port"><code>url.port</code></a></li>
164<li><a href="#url_url_protocol"><code>url.protocol</code></a>
165<ul>
166<li><a href="#url_special_schemes">Special schemes</a></li>
167</ul>
168</li>
169<li><a href="#url_url_search"><code>url.search</code></a></li>
170<li><a href="#url_url_searchparams"><code>url.searchParams</code></a></li>
171<li><a href="#url_url_username"><code>url.username</code></a></li>
172<li><a href="#url_url_tostring"><code>url.toString()</code></a></li>
173<li><a href="#url_url_tojson"><code>url.toJSON()</code></a></li>
174</ul>
175</li>
176<li><a href="#url_class_urlsearchparams">Class: <code>URLSearchParams</code></a>
177<ul>
178<li><a href="#url_new_urlsearchparams"><code>new URLSearchParams()</code></a></li>
179<li><a href="#url_new_urlsearchparams_string"><code>new URLSearchParams(string)</code></a></li>
180<li><a href="#url_new_urlsearchparams_obj"><code>new URLSearchParams(obj)</code></a></li>
181<li><a href="#url_new_urlsearchparams_iterable"><code>new URLSearchParams(iterable)</code></a></li>
182<li><a href="#url_urlsearchparams_append_name_value"><code>urlSearchParams.append(name, value)</code></a></li>
183<li><a href="#url_urlsearchparams_delete_name"><code>urlSearchParams.delete(name)</code></a></li>
184<li><a href="#url_urlsearchparams_entries"><code>urlSearchParams.entries()</code></a></li>
185<li><a href="#url_urlsearchparams_foreach_fn_thisarg"><code>urlSearchParams.forEach(fn[, thisArg])</code></a></li>
186<li><a href="#url_urlsearchparams_get_name"><code>urlSearchParams.get(name)</code></a></li>
187<li><a href="#url_urlsearchparams_getall_name"><code>urlSearchParams.getAll(name)</code></a></li>
188<li><a href="#url_urlsearchparams_has_name"><code>urlSearchParams.has(name)</code></a></li>
189<li><a href="#url_urlsearchparams_keys"><code>urlSearchParams.keys()</code></a></li>
190<li><a href="#url_urlsearchparams_set_name_value"><code>urlSearchParams.set(name, value)</code></a></li>
191<li><a href="#url_urlsearchparams_sort"><code>urlSearchParams.sort()</code></a></li>
192<li><a href="#url_urlsearchparams_tostring"><code>urlSearchParams.toString()</code></a></li>
193<li><a href="#url_urlsearchparams_values"><code>urlSearchParams.values()</code></a></li>
194<li><a href="#url_urlsearchparams_symbol_iterator"><code>urlSearchParams[Symbol.iterator]()</code></a></li>
195</ul>
196</li>
197<li><a href="#url_url_domaintoascii_domain"><code>url.domainToASCII(domain)</code></a></li>
198<li><a href="#url_url_domaintounicode_domain"><code>url.domainToUnicode(domain)</code></a></li>
199<li><a href="#url_url_fileurltopath_url"><code>url.fileURLToPath(url)</code></a></li>
200<li><a href="#url_url_format_url_options"><code>url.format(URL[, options])</code></a></li>
201<li><a href="#url_url_pathtofileurl_path"><code>url.pathToFileURL(path)</code></a></li>
202<li><a href="#url_url_urltohttpoptions_url"><code>url.urlToHttpOptions(url)</code></a></li>
203</ul>
204</li>
205<li><span class="stability_3"><a href="#url_legacy_url_api">Legacy URL API</a></span>
206<ul>
207<li><span class="stability_3"><a href="#url_legacy_urlobject">Legacy <code>urlObject</code></a></span>
208<ul>
209<li><a href="#url_urlobject_auth"><code>urlObject.auth</code></a></li>
210<li><a href="#url_urlobject_hash"><code>urlObject.hash</code></a></li>
211<li><a href="#url_urlobject_host"><code>urlObject.host</code></a></li>
212<li><a href="#url_urlobject_hostname"><code>urlObject.hostname</code></a></li>
213<li><a href="#url_urlobject_href"><code>urlObject.href</code></a></li>
214<li><a href="#url_urlobject_path"><code>urlObject.path</code></a></li>
215<li><a href="#url_urlobject_pathname"><code>urlObject.pathname</code></a></li>
216<li><a href="#url_urlobject_port"><code>urlObject.port</code></a></li>
217<li><a href="#url_urlobject_protocol"><code>urlObject.protocol</code></a></li>
218<li><a href="#url_urlobject_query"><code>urlObject.query</code></a></li>
219<li><a href="#url_urlobject_search"><code>urlObject.search</code></a></li>
220<li><a href="#url_urlobject_slashes"><code>urlObject.slashes</code></a></li>
221</ul>
222</li>
223<li><span class="stability_3"><a href="#url_url_format_urlobject"><code>url.format(urlObject)</code></a></span></li>
224<li><span class="stability_3"><a href="#url_url_parse_urlstring_parsequerystring_slashesdenotehost"><code>url.parse(urlString[, parseQueryString[, slashesDenoteHost]])</code></a></span></li>
225<li><span class="stability_3"><a href="#url_url_resolve_from_to"><code>url.resolve(from, to)</code></a></span></li>
226</ul>
227</li>
228<li><a href="#url_percent_encoding_in_urls">Percent-encoding in URLs</a>
229<ul>
230<li><a href="#url_legacy_api">Legacy API</a></li>
231<li><a href="#url_whatwg_api">WHATWG API</a></li>
232</ul>
233</li>
234</ul>
235</li>
236</ul></details>
237
238      <div id="apicontent">
239        <h2>URL<span><a class="mark" href="#url_url" id="url_url">#</a></span></h2>
240
241<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
242<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.19.1/lib/url.js">lib/url.js</a></p>
243<p>The <code>url</code> module provides utilities for URL resolution and parsing. It can be
244accessed using:</p>
245<pre><code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'url'</span>);</code></pre>
246<section><h3>URL strings and URL objects<span><a class="mark" href="#url_url_strings_and_url_objects" id="url_url_strings_and_url_objects">#</a></span></h3>
247<p>A URL string is a structured string containing multiple meaningful components.
248When parsed, a URL object is returned containing properties for each of these
249components.</p>
250<p>The <code>url</code> module provides two APIs for working with URLs: a legacy API that is
251Node.js specific, and a newer API that implements the same
252<a href="https://url.spec.whatwg.org/">WHATWG URL Standard</a> used by web browsers.</p>
253<p>A comparison between the WHATWG and Legacy APIs is provided below. Above the URL
254<code>'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'</code>, properties
255of an object returned by the legacy <code>url.parse()</code> are shown. Below it are
256properties of a WHATWG <code>URL</code> object.</p>
257<p>WHATWG URL's <code>origin</code> property includes <code>protocol</code> and <code>host</code>, but not
258<code>username</code> or <code>password</code>.</p>
259<pre><code class="language-text">┌────────────────────────────────────────────────────────────────────────────────────────────────┐
260│                                              href                                              │
261├──────────┬──┬─────────────────────┬────────────────────────┬───────────────────────────┬───────┤
262│ protocol │  │        auth         │          host          │           path            │ hash  │
263│          │  │                     ├─────────────────┬──────┼──────────┬────────────────┤       │
264│          │  │                     │    hostname     │ port │ pathname │     search     │       │
265│          │  │                     │                 │      │          ├─┬──────────────┤       │
266│          │  │                     │                 │      │          │ │    query     │       │
267"  https:   //    user   :   pass   @ sub.example.com : 8080   /p/a/t/h  ?  query=string   #hash "
268│          │  │          │          │    hostname     │ port │          │                │       │
269│          │  │          │          ├─────────────────┴──────┤          │                │       │
270│ protocol │  │ username │ password │          host          │          │                │       │
271├──────────┴──┼──────────┴──────────┼────────────────────────┤          │                │       │
272│   origin    │                     │         origin         │ pathname │     search     │ hash  │
273├─────────────┴─────────────────────┴────────────────────────┴──────────┴────────────────┴───────┤
274│                                              href                                              │
275└────────────────────────────────────────────────────────────────────────────────────────────────┘
276(All spaces in the "" line should be ignored. They are purely for formatting.)</code></pre>
277<p>Parsing the URL string using the WHATWG API:</p>
278<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL =
279  <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'</span>);</code></pre>
280<p>Parsing the URL string using the Legacy API:</p>
281<pre><code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'url'</span>);
282<span class="hljs-keyword">const</span> myURL =
283  url.<span class="hljs-title function_">parse</span>(<span class="hljs-string">'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'</span>);</code></pre>
284<h4>Constructing a URL from component parts and getting the constructed string<span><a class="mark" href="#url_constructing_a_url_from_component_parts_and_getting_the_constructed_string" id="url_constructing_a_url_from_component_parts_and_getting_the_constructed_string">#</a></span></h4>
285<p>It is possible to construct a WHATWG URL from component parts using either the
286property setters or a template literal string:</p>
287<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org'</span>);
288myURL.<span class="hljs-property">pathname</span> = <span class="hljs-string">'/a/b/c'</span>;
289myURL.<span class="hljs-property">search</span> = <span class="hljs-string">'?d=e'</span>;
290myURL.<span class="hljs-property">hash</span> = <span class="hljs-string">'#fgh'</span>;</code></pre>
291<pre><code class="language-js"><span class="hljs-keyword">const</span> pathname = <span class="hljs-string">'/a/b/c'</span>;
292<span class="hljs-keyword">const</span> search = <span class="hljs-string">'?d=e'</span>;
293<span class="hljs-keyword">const</span> hash = <span class="hljs-string">'#fgh'</span>;
294<span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">`https://example.org<span class="hljs-subst">${pathname}</span><span class="hljs-subst">${search}</span><span class="hljs-subst">${hash}</span>`</span>);</code></pre>
295<p>To get the constructed URL string, use the <code>href</code> property accessor:</p>
296<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);</code></pre>
297</section><section><h3>The WHATWG URL API<span><a class="mark" href="#url_the_whatwg_url_api" id="url_the_whatwg_url_api">#</a></span></h3>
298<h4>Class: <code>URL</code><span><a class="mark" href="#url_class_url" id="url_class_url">#</a></span></h4>
299<div class="api_metadata">
300<details class="changelog"><summary>History</summary>
301<table>
302<tbody><tr><th>Version</th><th>Changes</th></tr>
303<tr><td>v10.0.0</td>
304<td><p>The class is now available on the global object.</p></td></tr>
305<tr><td>v7.0.0, v6.13.0</td>
306<td><p><span>Added in: v7.0.0, v6.13.0</span></p></td></tr>
307</tbody></table>
308</details>
309</div>
310<p>Browser-compatible <code>URL</code> class, implemented by following the WHATWG URL
311Standard. <a href="https://url.spec.whatwg.org/#example-url-parsing">Examples of parsed URLs</a> may be found in the Standard itself.
312The <code>URL</code> class is also available on the global object.</p>
313<p>In accordance with browser conventions, all properties of <code>URL</code> objects
314are implemented as getters and setters on the class prototype, rather than as
315data properties on the object itself. Thus, unlike <a href="#url_legacy_urlobject">legacy <code>urlObject</code></a>s,
316using the <code>delete</code> keyword on any properties of <code>URL</code> objects (e.g. <code>delete myURL.protocol</code>, <code>delete myURL.pathname</code>, etc) has no effect but will still
317return <code>true</code>.</p>
318<h5><code>new URL(input[, base])</code><span><a class="mark" href="#url_new_url_input_base" id="url_new_url_input_base">#</a></span></h5>
319<ul>
320<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The absolute or relative input URL to parse. If <code>input</code>
321is relative, then <code>base</code> is required. If <code>input</code> is absolute, the <code>base</code>
322is ignored.</li>
323<li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#url_the_whatwg_url_api" class="type">&#x3C;URL></a> The base URL to resolve against if the <code>input</code> is not
324absolute.</li>
325</ul>
326<p>Creates a new <code>URL</code> object by parsing the <code>input</code> relative to the <code>base</code>. If
327<code>base</code> is passed as a string, it will be parsed equivalent to <code>new URL(base)</code>.</p>
328<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/foo'</span>, <span class="hljs-string">'https://example.org/'</span>);
329<span class="hljs-comment">// https://example.org/foo</span></code></pre>
330<p>The URL constructor is accessible as a property on the global object.
331It can also be imported from the built-in url module:</p>
332<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable constant_">URL</span> === <span class="hljs-built_in">require</span>(<span class="hljs-string">'url'</span>).<span class="hljs-property">URL</span>); <span class="hljs-comment">// Prints 'true'.</span></code></pre>
333<p>A <code>TypeError</code> will be thrown if the <code>input</code> or <code>base</code> are not valid URLs. Note
334that an effort will be made to coerce the given values into strings. For
335instance:</p>
336<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>({ <span class="hljs-attr">toString</span>: <span class="hljs-function">() =></span> <span class="hljs-string">'https://example.org/'</span> });
337<span class="hljs-comment">// https://example.org/</span></code></pre>
338<p>Unicode characters appearing within the host name of <code>input</code> will be
339automatically converted to ASCII using the <a href="https://tools.ietf.org/html/rfc5891#section-4.4">Punycode</a> algorithm.</p>
340<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://測試'</span>);
341<span class="hljs-comment">// https://xn--g6w251d/</span></code></pre>
342<p>This feature is only available if the <code>node</code> executable was compiled with
343<a href="intl.html#intl_options_for_building_node_js">ICU</a> enabled. If not, the domain names are passed through unchanged.</p>
344<p>In cases where it is not known in advance if <code>input</code> is an absolute URL
345and a <code>base</code> is provided, it is advised to validate that the <code>origin</code> of
346the <code>URL</code> object is what is expected.</p>
347<pre><code class="language-js"><span class="hljs-keyword">let</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'http://Example.com/'</span>, <span class="hljs-string">'https://example.org/'</span>);
348<span class="hljs-comment">// http://example.com/</span>
349
350myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://Example.com/'</span>, <span class="hljs-string">'https://example.org/'</span>);
351<span class="hljs-comment">// https://example.com/</span>
352
353myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'foo://Example.com/'</span>, <span class="hljs-string">'https://example.org/'</span>);
354<span class="hljs-comment">// foo://Example.com/</span>
355
356myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'http:Example.com/'</span>, <span class="hljs-string">'https://example.org/'</span>);
357<span class="hljs-comment">// http://example.com/</span>
358
359myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https:Example.com/'</span>, <span class="hljs-string">'https://example.org/'</span>);
360<span class="hljs-comment">// https://example.org/Example.com/</span>
361
362myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'foo:Example.com/'</span>, <span class="hljs-string">'https://example.org/'</span>);
363<span class="hljs-comment">// foo:Example.com/</span></code></pre>
364<h5><code>url.hash</code><span><a class="mark" href="#url_url_hash" id="url_url_hash">#</a></span></h5>
365<ul>
366<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
367</ul>
368<p>Gets and sets the fragment portion of the URL.</p>
369<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org/foo#bar'</span>);
370<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">hash</span>);
371<span class="hljs-comment">// Prints #bar</span>
372
373myURL.<span class="hljs-property">hash</span> = <span class="hljs-string">'baz'</span>;
374<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
375<span class="hljs-comment">// Prints https://example.org/foo#baz</span></code></pre>
376<p>Invalid URL characters included in the value assigned to the <code>hash</code> property
377are <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which characters to
378percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_parsequerystring_slashesdenotehost"><code>url.parse()</code></a> and
379<a href="#url_url_format_urlobject"><code>url.format()</code></a> methods would produce.</p>
380<h5><code>url.host</code><span><a class="mark" href="#url_url_host" id="url_url_host">#</a></span></h5>
381<ul>
382<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
383</ul>
384<p>Gets and sets the host portion of the URL.</p>
385<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org:81/foo'</span>);
386<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">host</span>);
387<span class="hljs-comment">// Prints example.org:81</span>
388
389myURL.<span class="hljs-property">host</span> = <span class="hljs-string">'example.com:82'</span>;
390<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
391<span class="hljs-comment">// Prints https://example.com:82/foo</span></code></pre>
392<p>Invalid host values assigned to the <code>host</code> property are ignored.</p>
393<h5><code>url.hostname</code><span><a class="mark" href="#url_url_hostname" id="url_url_hostname">#</a></span></h5>
394<ul>
395<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
396</ul>
397<p>Gets and sets the host name portion of the URL. The key difference between
398<code>url.host</code> and <code>url.hostname</code> is that <code>url.hostname</code> does <em>not</em> include the
399port.</p>
400<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org:81/foo'</span>);
401<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">hostname</span>);
402<span class="hljs-comment">// Prints example.org</span>
403
404<span class="hljs-comment">// Setting the hostname does not change the port</span>
405myURL.<span class="hljs-property">hostname</span> = <span class="hljs-string">'example.com:82'</span>;
406<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
407<span class="hljs-comment">// Prints https://example.com:81/foo</span>
408
409<span class="hljs-comment">// Use myURL.host to change the hostname and port</span>
410myURL.<span class="hljs-property">host</span> = <span class="hljs-string">'example.org:82'</span>;
411<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
412<span class="hljs-comment">// Prints https://example.org:82/foo</span></code></pre>
413<p>Invalid host name values assigned to the <code>hostname</code> property are ignored.</p>
414<h5><code>url.href</code><span><a class="mark" href="#url_url_href" id="url_url_href">#</a></span></h5>
415<ul>
416<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
417</ul>
418<p>Gets and sets the serialized URL.</p>
419<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org/foo'</span>);
420<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
421<span class="hljs-comment">// Prints https://example.org/foo</span>
422
423myURL.<span class="hljs-property">href</span> = <span class="hljs-string">'https://example.com/bar'</span>;
424<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
425<span class="hljs-comment">// Prints https://example.com/bar</span></code></pre>
426<p>Getting the value of the <code>href</code> property is equivalent to calling
427<a href="#url_url_tostring"><code>url.toString()</code></a>.</p>
428<p>Setting the value of this property to a new value is equivalent to creating a
429new <code>URL</code> object using <a href="#url_new_url_input_base"><code>new URL(value)</code></a>. Each of the <code>URL</code>
430object's properties will be modified.</p>
431<p>If the value assigned to the <code>href</code> property is not a valid URL, a <code>TypeError</code>
432will be thrown.</p>
433<h5><code>url.origin</code><span><a class="mark" href="#url_url_origin" id="url_url_origin">#</a></span></h5>
434<ul>
435<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
436</ul>
437<p>Gets the read-only serialization of the URL's origin.</p>
438<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org/foo/bar?baz'</span>);
439<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">origin</span>);
440<span class="hljs-comment">// Prints https://example.org</span></code></pre>
441<pre><code class="language-js"><span class="hljs-keyword">const</span> idnURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://測試'</span>);
442<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(idnURL.<span class="hljs-property">origin</span>);
443<span class="hljs-comment">// Prints https://xn--g6w251d</span>
444
445<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(idnURL.<span class="hljs-property">hostname</span>);
446<span class="hljs-comment">// Prints xn--g6w251d</span></code></pre>
447<h5><code>url.password</code><span><a class="mark" href="#url_url_password" id="url_url_password">#</a></span></h5>
448<ul>
449<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
450</ul>
451<p>Gets and sets the password portion of the URL.</p>
452<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://abc:xyz@example.com'</span>);
453<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">password</span>);
454<span class="hljs-comment">// Prints xyz</span>
455
456myURL.<span class="hljs-property">password</span> = <span class="hljs-string">'123'</span>;
457<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
458<span class="hljs-comment">// Prints https://abc:123@example.com</span></code></pre>
459<p>Invalid URL characters included in the value assigned to the <code>password</code> property
460are <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which characters to
461percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_parsequerystring_slashesdenotehost"><code>url.parse()</code></a> and
462<a href="#url_url_format_urlobject"><code>url.format()</code></a> methods would produce.</p>
463<h5><code>url.pathname</code><span><a class="mark" href="#url_url_pathname" id="url_url_pathname">#</a></span></h5>
464<ul>
465<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
466</ul>
467<p>Gets and sets the path portion of the URL.</p>
468<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org/abc/xyz?123'</span>);
469<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">pathname</span>);
470<span class="hljs-comment">// Prints /abc/xyz</span>
471
472myURL.<span class="hljs-property">pathname</span> = <span class="hljs-string">'/abcdef'</span>;
473<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
474<span class="hljs-comment">// Prints https://example.org/abcdef?123</span></code></pre>
475<p>Invalid URL characters included in the value assigned to the <code>pathname</code>
476property are <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which characters
477to percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_parsequerystring_slashesdenotehost"><code>url.parse()</code></a> and
478<a href="#url_url_format_urlobject"><code>url.format()</code></a> methods would produce.</p>
479<h5><code>url.port</code><span><a class="mark" href="#url_url_port" id="url_url_port">#</a></span></h5>
480<ul>
481<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
482</ul>
483<p>Gets and sets the port portion of the URL.</p>
484<p>The port value may be a number or a string containing a number in the range
485<code>0</code> to <code>65535</code> (inclusive). Setting the value to the default port of the
486<code>URL</code> objects given <code>protocol</code> will result in the <code>port</code> value becoming
487the empty string (<code>''</code>).</p>
488<p>The port value can be an empty string in which case the port depends on
489the protocol/scheme:</p>
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527<table><thead><tr><th>protocol</th><th>port</th></tr></thead><tbody><tr><td>"ftp"</td><td>21</td></tr><tr><td>"file"</td><td></td></tr><tr><td>"gopher"</td><td>70</td></tr><tr><td>"http"</td><td>80</td></tr><tr><td>"https"</td><td>443</td></tr><tr><td>"ws"</td><td>80</td></tr><tr><td>"wss"</td><td>443</td></tr></tbody></table>
528<p>Upon assigning a value to the port, the value will first be converted to a
529string using <code>.toString()</code>.</p>
530<p>If that string is invalid but it begins with a number, the leading number is
531assigned to <code>port</code>.
532If the number lies outside the range denoted above, it is ignored.</p>
533<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org:8888'</span>);
534<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
535<span class="hljs-comment">// Prints 8888</span>
536
537<span class="hljs-comment">// Default ports are automatically transformed to the empty string</span>
538<span class="hljs-comment">// (HTTPS protocol's default port is 443)</span>
539myURL.<span class="hljs-property">port</span> = <span class="hljs-string">'443'</span>;
540<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
541<span class="hljs-comment">// Prints the empty string</span>
542<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
543<span class="hljs-comment">// Prints https://example.org/</span>
544
545myURL.<span class="hljs-property">port</span> = <span class="hljs-number">1234</span>;
546<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
547<span class="hljs-comment">// Prints 1234</span>
548<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
549<span class="hljs-comment">// Prints https://example.org:1234/</span>
550
551<span class="hljs-comment">// Completely invalid port strings are ignored</span>
552myURL.<span class="hljs-property">port</span> = <span class="hljs-string">'abcd'</span>;
553<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
554<span class="hljs-comment">// Prints 1234</span>
555
556<span class="hljs-comment">// Leading numbers are treated as a port number</span>
557myURL.<span class="hljs-property">port</span> = <span class="hljs-string">'5678abcd'</span>;
558<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
559<span class="hljs-comment">// Prints 5678</span>
560
561<span class="hljs-comment">// Non-integers are truncated</span>
562myURL.<span class="hljs-property">port</span> = <span class="hljs-number">1234.5678</span>;
563<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
564<span class="hljs-comment">// Prints 1234</span>
565
566<span class="hljs-comment">// Out-of-range numbers which are not represented in scientific notation</span>
567<span class="hljs-comment">// will be ignored.</span>
568myURL.<span class="hljs-property">port</span> = <span class="hljs-number">1e10</span>; <span class="hljs-comment">// 10000000000, will be range-checked as described below</span>
569<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
570<span class="hljs-comment">// Prints 1234</span></code></pre>
571<p>Numbers which contain a decimal point,
572such as floating-point numbers or numbers in scientific notation,
573are not an exception to this rule.
574Leading numbers up to the decimal point will be set as the URL's port,
575assuming they are valid:</p>
576<pre><code class="language-js">myURL.<span class="hljs-property">port</span> = <span class="hljs-number">4.567e21</span>;
577<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
578<span class="hljs-comment">// Prints 4 (because it is the leading number in the string '4.567e21')</span></code></pre>
579<h5><code>url.protocol</code><span><a class="mark" href="#url_url_protocol" id="url_url_protocol">#</a></span></h5>
580<ul>
581<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
582</ul>
583<p>Gets and sets the protocol portion of the URL.</p>
584<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org'</span>);
585<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">protocol</span>);
586<span class="hljs-comment">// Prints https:</span>
587
588myURL.<span class="hljs-property">protocol</span> = <span class="hljs-string">'ftp'</span>;
589<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
590<span class="hljs-comment">// Prints ftp://example.org/</span></code></pre>
591<p>Invalid URL protocol values assigned to the <code>protocol</code> property are ignored.</p>
592<h6>Special schemes<span><a class="mark" href="#url_special_schemes" id="url_special_schemes">#</a></span></h6>
593<p>The <a href="https://url.spec.whatwg.org/">WHATWG URL Standard</a> considers a handful of URL protocol schemes to be
594<em>special</em> in terms of how they are parsed and serialized. When a URL is
595parsed using one of these special protocols, the <code>url.protocol</code> property
596may be changed to another special protocol but cannot be changed to a
597non-special protocol, and vice versa.</p>
598<p>For instance, changing from <code>http</code> to <code>https</code> works:</p>
599<pre><code class="language-js"><span class="hljs-keyword">const</span> u = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'http://example.org'</span>);
600u.<span class="hljs-property">protocol</span> = <span class="hljs-string">'https'</span>;
601<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(u.<span class="hljs-property">href</span>);
602<span class="hljs-comment">// https://example.org</span></code></pre>
603<p>However, changing from <code>http</code> to a hypothetical <code>fish</code> protocol does not
604because the new protocol is not special.</p>
605<pre><code class="language-js"><span class="hljs-keyword">const</span> u = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'http://example.org'</span>);
606u.<span class="hljs-property">protocol</span> = <span class="hljs-string">'fish'</span>;
607<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(u.<span class="hljs-property">href</span>);
608<span class="hljs-comment">// http://example.org</span></code></pre>
609<p>Likewise, changing from a non-special protocol to a special protocol is also
610not permitted:</p>
611<pre><code class="language-js"><span class="hljs-keyword">const</span> u = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'fish://example.org'</span>);
612u.<span class="hljs-property">protocol</span> = <span class="hljs-string">'http'</span>;
613<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(u.<span class="hljs-property">href</span>);
614<span class="hljs-comment">// fish://example.org</span></code></pre>
615<p>According to the WHATWG URL Standard, special protocol schemes are <code>ftp</code>,
616<code>file</code>, <code>gopher</code>, <code>http</code>, <code>https</code>, <code>ws</code>, and <code>wss</code>.</p>
617<h5><code>url.search</code><span><a class="mark" href="#url_url_search" id="url_url_search">#</a></span></h5>
618<ul>
619<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
620</ul>
621<p>Gets and sets the serialized query portion of the URL.</p>
622<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org/abc?123'</span>);
623<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">search</span>);
624<span class="hljs-comment">// Prints ?123</span>
625
626myURL.<span class="hljs-property">search</span> = <span class="hljs-string">'abc=xyz'</span>;
627<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
628<span class="hljs-comment">// Prints https://example.org/abc?abc=xyz</span></code></pre>
629<p>Any invalid URL characters appearing in the value assigned the <code>search</code>
630property will be <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which
631characters to percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_parsequerystring_slashesdenotehost"><code>url.parse()</code></a>
632and <a href="#url_url_format_urlobject"><code>url.format()</code></a> methods would produce.</p>
633<h5><code>url.searchParams</code><span><a class="mark" href="#url_url_searchparams" id="url_url_searchparams">#</a></span></h5>
634<ul>
635<li><a href="url.html#url_class_urlsearchparams" class="type">&#x3C;URLSearchParams></a></li>
636</ul>
637<p>Gets the <a href="#url_class_urlsearchparams"><code>URLSearchParams</code></a> object representing the query parameters of the
638URL. This property is read-only but the <code>URLSearchParams</code> object it provides
639can be used to mutate the URL instance; to replace the entirety of query
640parameters of the URL, use the <a href="#url_url_search"><code>url.search</code></a> setter. See
641<a href="#url_class_urlsearchparams"><code>URLSearchParams</code></a> documentation for details.</p>
642<p>Use care when using <code>.searchParams</code> to modify the <code>URL</code> because,
643per the WHATWG specification, the <code>URLSearchParams</code> object uses
644different rules to determine which characters to percent-encode. For
645instance, the <code>URL</code> object will not percent encode the ASCII tilde (<code>~</code>)
646character, while <code>URLSearchParams</code> will always encode it:</p>
647<pre><code class="language-js"><span class="hljs-keyword">const</span> myUrl = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org/abc?foo=~bar'</span>);
648
649<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myUrl.<span class="hljs-property">search</span>);  <span class="hljs-comment">// prints ?foo=~bar</span>
650
651<span class="hljs-comment">// Modify the URL via searchParams...</span>
652myUrl.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">sort</span>();
653
654<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myUrl.<span class="hljs-property">search</span>);  <span class="hljs-comment">// prints ?foo=%7Ebar</span></code></pre>
655<h5><code>url.username</code><span><a class="mark" href="#url_url_username" id="url_url_username">#</a></span></h5>
656<ul>
657<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
658</ul>
659<p>Gets and sets the username portion of the URL.</p>
660<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://abc:xyz@example.com'</span>);
661<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">username</span>);
662<span class="hljs-comment">// Prints abc</span>
663
664myURL.<span class="hljs-property">username</span> = <span class="hljs-string">'123'</span>;
665<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
666<span class="hljs-comment">// Prints https://123:xyz@example.com/</span></code></pre>
667<p>Any invalid URL characters appearing in the value assigned the <code>username</code>
668property will be <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which
669characters to percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_parsequerystring_slashesdenotehost"><code>url.parse()</code></a>
670and <a href="#url_url_format_urlobject"><code>url.format()</code></a> methods would produce.</p>
671<h5><code>url.toString()</code><span><a class="mark" href="#url_url_tostring" id="url_url_tostring">#</a></span></h5>
672<ul>
673<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
674</ul>
675<p>The <code>toString()</code> method on the <code>URL</code> object returns the serialized URL. The
676value returned is equivalent to that of <a href="#url_url_href"><code>url.href</code></a> and <a href="#url_url_tojson"><code>url.toJSON()</code></a>.</p>
677<p>Because of the need for standard compliance, this method does not allow users
678to customize the serialization process of the URL. For more flexibility,
679<a href="#url_url_format_url_options"><code>require('url').format()</code></a> method might be of interest.</p>
680<h5><code>url.toJSON()</code><span><a class="mark" href="#url_url_tojson" id="url_url_tojson">#</a></span></h5>
681<ul>
682<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
683</ul>
684<p>The <code>toJSON()</code> method on the <code>URL</code> object returns the serialized URL. The
685value returned is equivalent to that of <a href="#url_url_href"><code>url.href</code></a> and
686<a href="#url_url_tostring"><code>url.toString()</code></a>.</p>
687<p>This method is automatically called when an <code>URL</code> object is serialized
688with <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify"><code>JSON.stringify()</code></a>.</p>
689<pre><code class="language-js"><span class="hljs-keyword">const</span> myURLs = [
690  <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://www.example.com'</span>),
691  <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://test.example.org'</span>),
692];
693<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable constant_">JSON</span>.<span class="hljs-title function_">stringify</span>(myURLs));
694<span class="hljs-comment">// Prints ["https://www.example.com/","https://test.example.org/"]</span></code></pre>
695<h4>Class: <code>URLSearchParams</code><span><a class="mark" href="#url_class_urlsearchparams" id="url_class_urlsearchparams">#</a></span></h4>
696<div class="api_metadata">
697<details class="changelog"><summary>History</summary>
698<table>
699<tbody><tr><th>Version</th><th>Changes</th></tr>
700<tr><td>v10.0.0</td>
701<td><p>The class is now available on the global object.</p></td></tr>
702<tr><td>v7.5.0, v6.13.0</td>
703<td><p><span>Added in: v7.5.0, v6.13.0</span></p></td></tr>
704</tbody></table>
705</details>
706</div>
707<p>The <code>URLSearchParams</code> API provides read and write access to the query of a
708<code>URL</code>. The <code>URLSearchParams</code> class can also be used standalone with one of the
709four following constructors.
710The <code>URLSearchParams</code> class is also available on the global object.</p>
711<p>The WHATWG <code>URLSearchParams</code> interface and the <a href="querystring.html"><code>querystring</code></a> module have
712similar purpose, but the purpose of the <a href="querystring.html"><code>querystring</code></a> module is more
713general, as it allows the customization of delimiter characters (<code>&#x26;</code> and <code>=</code>).
714On the other hand, this API is designed purely for URL query strings.</p>
715<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org/?abc=123'</span>);
716<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">get</span>(<span class="hljs-string">'abc'</span>));
717<span class="hljs-comment">// Prints 123</span>
718
719myURL.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">append</span>(<span class="hljs-string">'abc'</span>, <span class="hljs-string">'xyz'</span>);
720<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
721<span class="hljs-comment">// Prints https://example.org/?abc=123&#x26;abc=xyz</span>
722
723myURL.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">delete</span>(<span class="hljs-string">'abc'</span>);
724myURL.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">set</span>(<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>);
725<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
726<span class="hljs-comment">// Prints https://example.org/?a=b</span>
727
728<span class="hljs-keyword">const</span> newSearchParams = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</span>(myURL.<span class="hljs-property">searchParams</span>);
729<span class="hljs-comment">// The above is equivalent to</span>
730<span class="hljs-comment">// const newSearchParams = new URLSearchParams(myURL.search);</span>
731
732newSearchParams.<span class="hljs-title function_">append</span>(<span class="hljs-string">'a'</span>, <span class="hljs-string">'c'</span>);
733<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
734<span class="hljs-comment">// Prints https://example.org/?a=b</span>
735<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(newSearchParams.<span class="hljs-title function_">toString</span>());
736<span class="hljs-comment">// Prints a=b&#x26;a=c</span>
737
738<span class="hljs-comment">// newSearchParams.toString() is implicitly called</span>
739myURL.<span class="hljs-property">search</span> = newSearchParams;
740<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
741<span class="hljs-comment">// Prints https://example.org/?a=b&#x26;a=c</span>
742newSearchParams.<span class="hljs-title function_">delete</span>(<span class="hljs-string">'a'</span>);
743<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
744<span class="hljs-comment">// Prints https://example.org/?a=b&#x26;a=c</span></code></pre>
745<h5><code>new URLSearchParams()</code><span><a class="mark" href="#url_new_urlsearchparams" id="url_new_urlsearchparams">#</a></span></h5>
746<p>Instantiate a new empty <code>URLSearchParams</code> object.</p>
747<h5><code>new URLSearchParams(string)</code><span><a class="mark" href="#url_new_urlsearchparams_string" id="url_new_urlsearchparams_string">#</a></span></h5>
748<ul>
749<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> A query string</li>
750</ul>
751<p>Parse the <code>string</code> as a query string, and use it to instantiate a new
752<code>URLSearchParams</code> object. A leading <code>'?'</code>, if present, is ignored.</p>
753<pre><code class="language-js"><span class="hljs-keyword">let</span> params;
754
755params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</span>(<span class="hljs-string">'user=abc&#x26;query=xyz'</span>);
756<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">get</span>(<span class="hljs-string">'user'</span>));
757<span class="hljs-comment">// Prints 'abc'</span>
758<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
759<span class="hljs-comment">// Prints 'user=abc&#x26;query=xyz'</span>
760
761params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</span>(<span class="hljs-string">'?user=abc&#x26;query=xyz'</span>);
762<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
763<span class="hljs-comment">// Prints 'user=abc&#x26;query=xyz'</span></code></pre>
764<h5><code>new URLSearchParams(obj)</code><span><a class="mark" href="#url_new_urlsearchparams_obj" id="url_new_urlsearchparams_obj">#</a></span></h5>
765<div class="api_metadata">
766<span>Added in: v7.10.0, v6.13.0</span>
767</div>
768<ul>
769<li><code>obj</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> An object representing a collection of key-value pairs</li>
770</ul>
771<p>Instantiate a new <code>URLSearchParams</code> object with a query hash map. The key and
772value of each property of <code>obj</code> are always coerced to strings.</p>
773<p>Unlike <a href="querystring.html"><code>querystring</code></a> module, duplicate keys in the form of array values are
774not allowed. Arrays are stringified using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString"><code>array.toString()</code></a>, which simply
775joins all array elements with commas.</p>
776<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</span>({
777  <span class="hljs-attr">user</span>: <span class="hljs-string">'abc'</span>,
778  <span class="hljs-attr">query</span>: [<span class="hljs-string">'first'</span>, <span class="hljs-string">'second'</span>]
779});
780<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">getAll</span>(<span class="hljs-string">'query'</span>));
781<span class="hljs-comment">// Prints [ 'first,second' ]</span>
782<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
783<span class="hljs-comment">// Prints 'user=abc&#x26;query=first%2Csecond'</span></code></pre>
784<h5><code>new URLSearchParams(iterable)</code><span><a class="mark" href="#url_new_urlsearchparams_iterable" id="url_new_urlsearchparams_iterable">#</a></span></h5>
785<div class="api_metadata">
786<span>Added in: v7.10.0, v6.13.0</span>
787</div>
788<ul>
789<li><code>iterable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type">&#x3C;Iterable></a> An iterable object whose elements are key-value pairs</li>
790</ul>
791<p>Instantiate a new <code>URLSearchParams</code> object with an iterable map in a way that
792is similar to <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><code>Map</code></a>'s constructor. <code>iterable</code> can be an <code>Array</code> or any
793iterable object. That means <code>iterable</code> can be another <code>URLSearchParams</code>, in
794which case the constructor will simply create a clone of the provided
795<code>URLSearchParams</code>. Elements of <code>iterable</code> are key-value pairs, and can
796themselves be any iterable object.</p>
797<p>Duplicate keys are allowed.</p>
798<pre><code class="language-js"><span class="hljs-keyword">let</span> params;
799
800<span class="hljs-comment">// Using an array</span>
801params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</span>([
802  [<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>],
803  [<span class="hljs-string">'query'</span>, <span class="hljs-string">'first'</span>],
804  [<span class="hljs-string">'query'</span>, <span class="hljs-string">'second'</span>],
805]);
806<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
807<span class="hljs-comment">// Prints 'user=abc&#x26;query=first&#x26;query=second'</span>
808
809<span class="hljs-comment">// Using a Map object</span>
810<span class="hljs-keyword">const</span> map = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>();
811map.<span class="hljs-title function_">set</span>(<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>);
812map.<span class="hljs-title function_">set</span>(<span class="hljs-string">'query'</span>, <span class="hljs-string">'xyz'</span>);
813params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</span>(map);
814<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
815<span class="hljs-comment">// Prints 'user=abc&#x26;query=xyz'</span>
816
817<span class="hljs-comment">// Using a generator function</span>
818<span class="hljs-keyword">function</span>* <span class="hljs-title function_">getQueryPairs</span>(<span class="hljs-params"></span>) {
819  <span class="hljs-keyword">yield</span> [<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>];
820  <span class="hljs-keyword">yield</span> [<span class="hljs-string">'query'</span>, <span class="hljs-string">'first'</span>];
821  <span class="hljs-keyword">yield</span> [<span class="hljs-string">'query'</span>, <span class="hljs-string">'second'</span>];
822}
823params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</span>(<span class="hljs-title function_">getQueryPairs</span>());
824<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
825<span class="hljs-comment">// Prints 'user=abc&#x26;query=first&#x26;query=second'</span>
826
827<span class="hljs-comment">// Each key-value pair must have exactly two elements</span>
828<span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</span>([
829  [<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>, <span class="hljs-string">'error'</span>],
830]);
831<span class="hljs-comment">// Throws TypeError [ERR_INVALID_TUPLE]:</span>
832<span class="hljs-comment">//        Each query pair must be an iterable [name, value] tuple</span></code></pre>
833<h5><code>urlSearchParams.append(name, value)</code><span><a class="mark" href="#url_urlsearchparams_append_name_value" id="url_urlsearchparams_append_name_value">#</a></span></h5>
834<ul>
835<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
836<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
837</ul>
838<p>Append a new name-value pair to the query string.</p>
839<h5><code>urlSearchParams.delete(name)</code><span><a class="mark" href="#url_urlsearchparams_delete_name" id="url_urlsearchparams_delete_name">#</a></span></h5>
840<ul>
841<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
842</ul>
843<p>Remove all name-value pairs whose name is <code>name</code>.</p>
844<h5><code>urlSearchParams.entries()</code><span><a class="mark" href="#url_urlsearchparams_entries" id="url_urlsearchparams_entries">#</a></span></h5>
845<ul>
846<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type">&#x3C;Iterator></a></li>
847</ul>
848<p>Returns an ES6 <code>Iterator</code> over each of the name-value pairs in the query.
849Each item of the iterator is a JavaScript <code>Array</code>. The first item of the <code>Array</code>
850is the <code>name</code>, the second item of the <code>Array</code> is the <code>value</code>.</p>
851<p>Alias for <a href="#url_urlsearchparams_symbol_iterator"><code>urlSearchParams[@@iterator]()</code></a>.</p>
852<h5><code>urlSearchParams.forEach(fn[, thisArg])</code><span><a class="mark" href="#url_urlsearchparams_foreach_fn_thisarg" id="url_urlsearchparams_foreach_fn_thisarg">#</a></span></h5>
853<ul>
854<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Invoked for each name-value pair in the query</li>
855<li><code>thisArg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> To be used as <code>this</code> value for when <code>fn</code> is called</li>
856</ul>
857<p>Iterates over each name-value pair in the query and invokes the given function.</p>
858<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org/?a=b&#x26;c=d'</span>);
859myURL.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">value, name, searchParams</span>) =></span> {
860  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name, value, myURL.<span class="hljs-property">searchParams</span> === searchParams);
861});
862<span class="hljs-comment">// Prints:</span>
863<span class="hljs-comment">//   a b true</span>
864<span class="hljs-comment">//   c d true</span></code></pre>
865<h5><code>urlSearchParams.get(name)</code><span><a class="mark" href="#url_urlsearchparams_get_name" id="url_urlsearchparams_get_name">#</a></span></h5>
866<ul>
867<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
868<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> or <code>null</code> if there is no name-value pair with the given
869<code>name</code>.</li>
870</ul>
871<p>Returns the value of the first name-value pair whose name is <code>name</code>. If there
872are no such pairs, <code>null</code> is returned.</p>
873<h5><code>urlSearchParams.getAll(name)</code><span><a class="mark" href="#url_urlsearchparams_getall_name" id="url_urlsearchparams_getall_name">#</a></span></h5>
874<ul>
875<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
876<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
877</ul>
878<p>Returns the values of all name-value pairs whose name is <code>name</code>. If there are
879no such pairs, an empty array is returned.</p>
880<h5><code>urlSearchParams.has(name)</code><span><a class="mark" href="#url_urlsearchparams_has_name" id="url_urlsearchparams_has_name">#</a></span></h5>
881<ul>
882<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
883<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
884</ul>
885<p>Returns <code>true</code> if there is at least one name-value pair whose name is <code>name</code>.</p>
886<h5><code>urlSearchParams.keys()</code><span><a class="mark" href="#url_urlsearchparams_keys" id="url_urlsearchparams_keys">#</a></span></h5>
887<ul>
888<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type">&#x3C;Iterator></a></li>
889</ul>
890<p>Returns an ES6 <code>Iterator</code> over the names of each name-value pair.</p>
891<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</span>(<span class="hljs-string">'foo=bar&#x26;foo=baz'</span>);
892<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> name <span class="hljs-keyword">of</span> params.<span class="hljs-title function_">keys</span>()) {
893  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name);
894}
895<span class="hljs-comment">// Prints:</span>
896<span class="hljs-comment">//   foo</span>
897<span class="hljs-comment">//   foo</span></code></pre>
898<h5><code>urlSearchParams.set(name, value)</code><span><a class="mark" href="#url_urlsearchparams_set_name_value" id="url_urlsearchparams_set_name_value">#</a></span></h5>
899<ul>
900<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
901<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
902</ul>
903<p>Sets the value in the <code>URLSearchParams</code> object associated with <code>name</code> to
904<code>value</code>. If there are any pre-existing name-value pairs whose names are <code>name</code>,
905set the first such pair's value to <code>value</code> and remove all others. If not,
906append the name-value pair to the query string.</p>
907<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</span>();
908params.<span class="hljs-title function_">append</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>);
909params.<span class="hljs-title function_">append</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'baz'</span>);
910params.<span class="hljs-title function_">append</span>(<span class="hljs-string">'abc'</span>, <span class="hljs-string">'def'</span>);
911<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
912<span class="hljs-comment">// Prints foo=bar&#x26;foo=baz&#x26;abc=def</span>
913
914params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'def'</span>);
915params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'xyz'</span>, <span class="hljs-string">'opq'</span>);
916<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
917<span class="hljs-comment">// Prints foo=def&#x26;abc=def&#x26;xyz=opq</span></code></pre>
918<h5><code>urlSearchParams.sort()</code><span><a class="mark" href="#url_urlsearchparams_sort" id="url_urlsearchparams_sort">#</a></span></h5>
919<div class="api_metadata">
920<span>Added in: v7.7.0, v6.13.0</span>
921</div>
922<p>Sort all existing name-value pairs in-place by their names. Sorting is done
923with a <a href="https://en.wikipedia.org/wiki/Sorting_algorithm#Stability">stable sorting algorithm</a>, so relative order between name-value pairs
924with the same name is preserved.</p>
925<p>This method can be used, in particular, to increase cache hits.</p>
926<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</span>(<span class="hljs-string">'query[]=abc&#x26;type=search&#x26;query[]=123'</span>);
927params.<span class="hljs-title function_">sort</span>();
928<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
929<span class="hljs-comment">// Prints query%5B%5D=abc&#x26;query%5B%5D=123&#x26;type=search</span></code></pre>
930<h5><code>urlSearchParams.toString()</code><span><a class="mark" href="#url_urlsearchparams_tostring" id="url_urlsearchparams_tostring">#</a></span></h5>
931<ul>
932<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
933</ul>
934<p>Returns the search parameters serialized as a string, with characters
935percent-encoded where necessary.</p>
936<h5><code>urlSearchParams.values()</code><span><a class="mark" href="#url_urlsearchparams_values" id="url_urlsearchparams_values">#</a></span></h5>
937<ul>
938<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type">&#x3C;Iterator></a></li>
939</ul>
940<p>Returns an ES6 <code>Iterator</code> over the values of each name-value pair.</p>
941<h5><code>urlSearchParams[Symbol.iterator]()</code><span><a class="mark" href="#url_urlsearchparams_symbol_iterator" id="url_urlsearchparams_symbol_iterator">#</a></span></h5>
942<ul>
943<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type">&#x3C;Iterator></a></li>
944</ul>
945<p>Returns an ES6 <code>Iterator</code> over each of the name-value pairs in the query string.
946Each item of the iterator is a JavaScript <code>Array</code>. The first item of the <code>Array</code>
947is the <code>name</code>, the second item of the <code>Array</code> is the <code>value</code>.</p>
948<p>Alias for <a href="#url_urlsearchparams_entries"><code>urlSearchParams.entries()</code></a>.</p>
949<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</span>(<span class="hljs-string">'foo=bar&#x26;xyz=baz'</span>);
950<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> [name, value] <span class="hljs-keyword">of</span> params) {
951  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name, value);
952}
953<span class="hljs-comment">// Prints:</span>
954<span class="hljs-comment">//   foo bar</span>
955<span class="hljs-comment">//   xyz baz</span></code></pre>
956<h4><code>url.domainToASCII(domain)</code><span><a class="mark" href="#url_url_domaintoascii_domain" id="url_url_domaintoascii_domain">#</a></span></h4>
957<div class="api_metadata">
958<span>Added in: v7.4.0, v6.13.0</span>
959</div>
960<ul>
961<li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
962<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
963</ul>
964<p>Returns the <a href="https://tools.ietf.org/html/rfc5891#section-4.4">Punycode</a> ASCII serialization of the <code>domain</code>. If <code>domain</code> is an
965invalid domain, the empty string is returned.</p>
966<p>It performs the inverse operation to <a href="#url_url_domaintounicode_domain"><code>url.domainToUnicode()</code></a>.</p>
967<p>This feature is only available if the <code>node</code> executable was compiled with
968<a href="intl.html#intl_options_for_building_node_js">ICU</a> enabled. If not, the domain names are passed through unchanged.</p>
969<pre><code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'url'</span>);
970<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">domainToASCII</span>(<span class="hljs-string">'español.com'</span>));
971<span class="hljs-comment">// Prints xn--espaol-zwa.com</span>
972<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">domainToASCII</span>(<span class="hljs-string">'中文.com'</span>));
973<span class="hljs-comment">// Prints xn--fiq228c.com</span>
974<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">domainToASCII</span>(<span class="hljs-string">'xn--iñvalid.com'</span>));
975<span class="hljs-comment">// Prints an empty string</span></code></pre>
976<h4><code>url.domainToUnicode(domain)</code><span><a class="mark" href="#url_url_domaintounicode_domain" id="url_url_domaintounicode_domain">#</a></span></h4>
977<div class="api_metadata">
978<span>Added in: v7.4.0, v6.13.0</span>
979</div>
980<ul>
981<li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
982<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
983</ul>
984<p>Returns the Unicode serialization of the <code>domain</code>. If <code>domain</code> is an invalid
985domain, the empty string is returned.</p>
986<p>It performs the inverse operation to <a href="#url_url_domaintoascii_domain"><code>url.domainToASCII()</code></a>.</p>
987<p>This feature is only available if the <code>node</code> executable was compiled with
988<a href="intl.html#intl_options_for_building_node_js">ICU</a> enabled. If not, the domain names are passed through unchanged.</p>
989<pre><code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'url'</span>);
990<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">domainToUnicode</span>(<span class="hljs-string">'xn--espaol-zwa.com'</span>));
991<span class="hljs-comment">// Prints español.com</span>
992<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">domainToUnicode</span>(<span class="hljs-string">'xn--fiq228c.com'</span>));
993<span class="hljs-comment">// Prints 中文.com</span>
994<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">domainToUnicode</span>(<span class="hljs-string">'xn--iñvalid.com'</span>));
995<span class="hljs-comment">// Prints an empty string</span></code></pre>
996<h4><code>url.fileURLToPath(url)</code><span><a class="mark" href="#url_url_fileurltopath_url" id="url_url_fileurltopath_url">#</a></span></h4>
997<div class="api_metadata">
998<span>Added in: v10.12.0</span>
999</div>
1000<ul>
1001<li><code>url</code> <a href="url.html#url_the_whatwg_url_api" class="type">&#x3C;URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The file URL string or URL object to convert to a path.</li>
1002<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The fully-resolved platform-specific Node.js file path.</li>
1003</ul>
1004<p>This function ensures the correct decodings of percent-encoded characters as
1005well as ensuring a cross-platform valid absolute path string.</p>
1006
1007<pre><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { fileURLToPath } <span class="hljs-keyword">from</span> <span class="hljs-string">'url'</span>;
1008
1009<span class="hljs-keyword">const</span> __filename = <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-keyword">import</span>.<span class="hljs-property">meta</span>.<span class="hljs-property">url</span>);
1010
1011<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///C:/path/'</span>).<span class="hljs-property">pathname</span>;      <span class="hljs-comment">// Incorrect: /C:/path/</span>
1012<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///C:/path/'</span>);         <span class="hljs-comment">// Correct:   C:\path\ (Windows)</span>
1013
1014<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file://nas/foo.txt'</span>).<span class="hljs-property">pathname</span>;    <span class="hljs-comment">// Incorrect: /foo.txt</span>
1015<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file://nas/foo.txt'</span>);       <span class="hljs-comment">// Correct:   \\nas\foo.txt (Windows)</span>
1016
1017<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///你好.txt'</span>).<span class="hljs-property">pathname</span>;      <span class="hljs-comment">// Incorrect: /%E4%BD%A0%E5%A5%BD.txt</span>
1018<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///你好.txt'</span>);         <span class="hljs-comment">// Correct:   /你好.txt (POSIX)</span>
1019
1020<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///hello world'</span>).<span class="hljs-property">pathname</span>;   <span class="hljs-comment">// Incorrect: /hello%20world</span>
1021<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///hello world'</span>);      <span class="hljs-comment">// Correct:   /hello world (POSIX)</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { fileURLToPath } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'url'</span>);
1022<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///C:/path/'</span>).<span class="hljs-property">pathname</span>;      <span class="hljs-comment">// Incorrect: /C:/path/</span>
1023<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///C:/path/'</span>);         <span class="hljs-comment">// Correct:   C:\path\ (Windows)</span>
1024
1025<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file://nas/foo.txt'</span>).<span class="hljs-property">pathname</span>;    <span class="hljs-comment">// Incorrect: /foo.txt</span>
1026<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file://nas/foo.txt'</span>);       <span class="hljs-comment">// Correct:   \\nas\foo.txt (Windows)</span>
1027
1028<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///你好.txt'</span>).<span class="hljs-property">pathname</span>;      <span class="hljs-comment">// Incorrect: /%E4%BD%A0%E5%A5%BD.txt</span>
1029<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///你好.txt'</span>);         <span class="hljs-comment">// Correct:   /你好.txt (POSIX)</span>
1030
1031<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///hello world'</span>).<span class="hljs-property">pathname</span>;   <span class="hljs-comment">// Incorrect: /hello%20world</span>
1032<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///hello world'</span>);      <span class="hljs-comment">// Correct:   /hello world (POSIX)</span></code></pre>
1033<h4><code>url.format(URL[, options])</code><span><a class="mark" href="#url_url_format_url_options" id="url_url_format_url_options">#</a></span></h4>
1034<div class="api_metadata">
1035<span>Added in: v7.6.0</span>
1036</div>
1037<ul>
1038<li><code>URL</code> <a href="url.html#url_the_whatwg_url_api" class="type">&#x3C;URL></a> A <a href="#url_the_whatwg_url_api">WHATWG URL</a> object</li>
1039<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1040<ul>
1041<li><code>auth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> if the serialized URL string should include the
1042username and password, <code>false</code> otherwise. <strong>Default:</strong> <code>true</code>.</li>
1043<li><code>fragment</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> if the serialized URL string should include the
1044fragment, <code>false</code> otherwise. <strong>Default:</strong> <code>true</code>.</li>
1045<li><code>search</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> if the serialized URL string should include the
1046search query, <code>false</code> otherwise. <strong>Default:</strong> <code>true</code>.</li>
1047<li><code>unicode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> if Unicode characters appearing in the host
1048component of the URL string should be encoded directly as opposed to being
1049Punycode encoded. <strong>Default:</strong> <code>false</code>.</li>
1050</ul>
1051</li>
1052<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1053</ul>
1054<p>Returns a customizable serialization of a URL <code>String</code> representation of a
1055<a href="#url_the_whatwg_url_api">WHATWG URL</a> object.</p>
1056<p>The URL object has both a <code>toString()</code> method and <code>href</code> property that return
1057string serializations of the URL. These are not, however, customizable in
1058any way. The <code>url.format(URL[, options])</code> method allows for basic customization
1059of the output.</p>
1060
1061<pre><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> url <span class="hljs-keyword">from</span> <span class="hljs-string">'url'</span>;
1062<span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://a:b@測試?abc#foo'</span>);
1063
1064<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
1065<span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
1066
1067<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-title function_">toString</span>());
1068<span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
1069
1070<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">format</span>(myURL, { <span class="hljs-attr">fragment</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">unicode</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">auth</span>: <span class="hljs-literal">false</span> }));
1071<span class="hljs-comment">// Prints 'https://測試/?abc'</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'url'</span>);
1072<span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://a:b@測試?abc#foo'</span>);
1073
1074<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
1075<span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
1076
1077<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-title function_">toString</span>());
1078<span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
1079
1080<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">format</span>(myURL, { <span class="hljs-attr">fragment</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">unicode</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">auth</span>: <span class="hljs-literal">false</span> }));
1081<span class="hljs-comment">// Prints 'https://測試/?abc'</span></code></pre>
1082<h4><code>url.pathToFileURL(path)</code><span><a class="mark" href="#url_url_pathtofileurl_path" id="url_url_pathtofileurl_path">#</a></span></h4>
1083<div class="api_metadata">
1084<span>Added in: v10.12.0</span>
1085</div>
1086<ul>
1087<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The path to convert to a File URL.</li>
1088<li>Returns: <a href="url.html#url_the_whatwg_url_api" class="type">&#x3C;URL></a> The file URL object.</li>
1089</ul>
1090<p>This function ensures that <code>path</code> is resolved absolutely, and that the URL
1091control characters are correctly encoded when converting into a File URL.</p>
1092
1093<pre><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { pathToFileURL } <span class="hljs-keyword">from</span> <span class="hljs-string">'url'</span>;
1094
1095<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/foo#1'</span>, <span class="hljs-string">'file:'</span>);           <span class="hljs-comment">// Incorrect: file:///foo#1</span>
1096<span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/foo#1'</span>);              <span class="hljs-comment">// Correct:   file:///foo%231 (POSIX)</span>
1097
1098<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/some/path%.c'</span>, <span class="hljs-string">'file:'</span>);    <span class="hljs-comment">// Incorrect: file:///some/path%.c</span>
1099<span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/some/path%.c'</span>);       <span class="hljs-comment">// Correct:   file:///some/path%25.c (POSIX)</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { pathToFileURL } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'url'</span>);
1100<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(__filename);                  <span class="hljs-comment">// Incorrect: throws (POSIX)</span>
1101<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(__filename);                  <span class="hljs-comment">// Incorrect: C:\... (Windows)</span>
1102<span class="hljs-title function_">pathToFileURL</span>(__filename);            <span class="hljs-comment">// Correct:   file:///... (POSIX)</span>
1103<span class="hljs-title function_">pathToFileURL</span>(__filename);            <span class="hljs-comment">// Correct:   file:///C:/... (Windows)</span>
1104
1105<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/foo#1'</span>, <span class="hljs-string">'file:'</span>);           <span class="hljs-comment">// Incorrect: file:///foo#1</span>
1106<span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/foo#1'</span>);              <span class="hljs-comment">// Correct:   file:///foo%231 (POSIX)</span>
1107
1108<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/some/path%.c'</span>, <span class="hljs-string">'file:'</span>);    <span class="hljs-comment">// Incorrect: file:///some/path%.c</span>
1109<span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/some/path%.c'</span>);       <span class="hljs-comment">// Correct:   file:///some/path%25.c (POSIX)</span></code></pre>
1110<h4><code>url.urlToHttpOptions(url)</code><span><a class="mark" href="#url_url_urltohttpoptions_url" id="url_url_urltohttpoptions_url">#</a></span></h4>
1111<div class="api_metadata">
1112<span>Added in: v14.18.0</span>
1113</div>
1114<ul>
1115<li><code>url</code> <a href="url.html#url_the_whatwg_url_api" class="type">&#x3C;URL></a> The <a href="#url_the_whatwg_url_api">WHATWG URL</a> object to convert to an options object.</li>
1116<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Options object
1117<ul>
1118<li><code>protocol</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Protocol to use.</li>
1119<li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> A domain name or IP address of the server to issue the
1120request to.</li>
1121<li><code>hash</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The fragment portion of the URL.</li>
1122<li><code>search</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The serialized query portion of the URL.</li>
1123<li><code>pathname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The path portion of the URL.</li>
1124<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Request path. Should include query string if any.
1125E.G. <code>'/index.html?page=12'</code>. An exception is thrown when the request path
1126contains illegal characters. Currently, only spaces are rejected but that
1127may change in the future.</li>
1128<li><code>href</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The serialized URL.</li>
1129<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Port of remote server.</li>
1130<li><code>auth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Basic authentication i.e. <code>'user:password'</code> to compute an
1131Authorization header.</li>
1132</ul>
1133</li>
1134</ul>
1135<p>This utility function converts a URL object into an ordinary options object as
1136expected by the <a href="http.html#http_http_request_options_callback"><code>http.request()</code></a> and <a href="https.html#https_https_request_options_callback"><code>https.request()</code></a> APIs.</p>
1137<pre><code class="language-js"><span class="hljs-keyword">const</span> { urlToHttpOptions } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'url'</span>);
1138<span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://a:b@測試?abc#foo'</span>);
1139
1140<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">urlToHttpOptions</span>(myUrl));
1141<span class="hljs-comment">/**
1142{
1143  protocol: 'https:',
1144  hostname: 'xn--g6w251d',
1145  hash: '#foo',
1146  search: '?abc',
1147  pathname: '/',
1148  path: '/?abc',
1149  href: 'https://a:b<span class="hljs-doctag">@xn</span>--g6w251d/?abc#foo',
1150  auth: 'a:b'
1151}
1152*/</span></code></pre>
1153</section><section><h3>Legacy URL API<span><a class="mark" href="#url_legacy_url_api" id="url_legacy_url_api">#</a></span></h3>
1154<div class="api_metadata">
1155<details class="changelog"><summary>History</summary>
1156<table>
1157<tbody><tr><th>Version</th><th>Changes</th></tr>
1158<tr><td>v14.17.0</td>
1159<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr>
1160<tr><td>v11.0.0</td>
1161<td><p>This API is deprecated.</p></td></tr>
1162</tbody></table>
1163</details>
1164</div>
1165<p></p><div class="api_stability api_stability_3"><a href="documentation.html#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHATWG URL API instead.</div><p></p>
1166<h4>Legacy <code>urlObject</code><span><a class="mark" href="#url_legacy_urlobject" id="url_legacy_urlobject">#</a></span></h4>
1167<div class="api_metadata">
1168<details class="changelog"><summary>History</summary>
1169<table>
1170<tbody><tr><th>Version</th><th>Changes</th></tr>
1171<tr><td>v14.17.0</td>
1172<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr>
1173<tr><td>v11.0.0</td>
1174<td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p></td></tr>
1175</tbody></table>
1176</details>
1177</div>
1178<p></p><div class="api_stability api_stability_3"><a href="documentation.html#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHATWG URL API instead.</div><p></p>
1179<p>The legacy <code>urlObject</code> (<code>require('url').Url</code>) is created and returned by the
1180<code>url.parse()</code> function.</p>
1181<h5><code>urlObject.auth</code><span><a class="mark" href="#url_urlobject_auth" id="url_urlobject_auth">#</a></span></h5>
1182<p>The <code>auth</code> property is the username and password portion of the URL, also
1183referred to as <em>userinfo</em>. This string subset follows the <code>protocol</code> and
1184double slashes (if present) and precedes the <code>host</code> component, delimited by <code>@</code>.
1185The string is either the username, or it is the username and password separated
1186by <code>:</code>.</p>
1187<p>For example: <code>'user:pass'</code>.</p>
1188<h5><code>urlObject.hash</code><span><a class="mark" href="#url_urlobject_hash" id="url_urlobject_hash">#</a></span></h5>
1189<p>The <code>hash</code> property is the fragment identifier portion of the URL including the
1190leading <code>#</code> character.</p>
1191<p>For example: <code>'#hash'</code>.</p>
1192<h5><code>urlObject.host</code><span><a class="mark" href="#url_urlobject_host" id="url_urlobject_host">#</a></span></h5>
1193<p>The <code>host</code> property is the full lower-cased host portion of the URL, including
1194the <code>port</code> if specified.</p>
1195<p>For example: <code>'sub.example.com:8080'</code>.</p>
1196<h5><code>urlObject.hostname</code><span><a class="mark" href="#url_urlobject_hostname" id="url_urlobject_hostname">#</a></span></h5>
1197<p>The <code>hostname</code> property is the lower-cased host name portion of the <code>host</code>
1198component <em>without</em> the <code>port</code> included.</p>
1199<p>For example: <code>'sub.example.com'</code>.</p>
1200<h5><code>urlObject.href</code><span><a class="mark" href="#url_urlobject_href" id="url_urlobject_href">#</a></span></h5>
1201<p>The <code>href</code> property is the full URL string that was parsed with both the
1202<code>protocol</code> and <code>host</code> components converted to lower-case.</p>
1203<p>For example: <code>'http://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'</code>.</p>
1204<h5><code>urlObject.path</code><span><a class="mark" href="#url_urlobject_path" id="url_urlobject_path">#</a></span></h5>
1205<p>The <code>path</code> property is a concatenation of the <code>pathname</code> and <code>search</code>
1206components.</p>
1207<p>For example: <code>'/p/a/t/h?query=string'</code>.</p>
1208<p>No decoding of the <code>path</code> is performed.</p>
1209<h5><code>urlObject.pathname</code><span><a class="mark" href="#url_urlobject_pathname" id="url_urlobject_pathname">#</a></span></h5>
1210<p>The <code>pathname</code> property consists of the entire path section of the URL. This
1211is everything following the <code>host</code> (including the <code>port</code>) and before the start
1212of the <code>query</code> or <code>hash</code> components, delimited by either the ASCII question
1213mark (<code>?</code>) or hash (<code>#</code>) characters.</p>
1214<p>For example: <code>'/p/a/t/h'</code>.</p>
1215<p>No decoding of the path string is performed.</p>
1216<h5><code>urlObject.port</code><span><a class="mark" href="#url_urlobject_port" id="url_urlobject_port">#</a></span></h5>
1217<p>The <code>port</code> property is the numeric port portion of the <code>host</code> component.</p>
1218<p>For example: <code>'8080'</code>.</p>
1219<h5><code>urlObject.protocol</code><span><a class="mark" href="#url_urlobject_protocol" id="url_urlobject_protocol">#</a></span></h5>
1220<p>The <code>protocol</code> property identifies the URL's lower-cased protocol scheme.</p>
1221<p>For example: <code>'http:'</code>.</p>
1222<h5><code>urlObject.query</code><span><a class="mark" href="#url_urlobject_query" id="url_urlobject_query">#</a></span></h5>
1223<p>The <code>query</code> property is either the query string without the leading ASCII
1224question mark (<code>?</code>), or an object returned by the <a href="querystring.html"><code>querystring</code></a> module's
1225<code>parse()</code> method. Whether the <code>query</code> property is a string or object is
1226determined by the <code>parseQueryString</code> argument passed to <code>url.parse()</code>.</p>
1227<p>For example: <code>'query=string'</code> or <code>{'query': 'string'}</code>.</p>
1228<p>If returned as a string, no decoding of the query string is performed. If
1229returned as an object, both keys and values are decoded.</p>
1230<h5><code>urlObject.search</code><span><a class="mark" href="#url_urlobject_search" id="url_urlobject_search">#</a></span></h5>
1231<p>The <code>search</code> property consists of the entire "query string" portion of the
1232URL, including the leading ASCII question mark (<code>?</code>) character.</p>
1233<p>For example: <code>'?query=string'</code>.</p>
1234<p>No decoding of the query string is performed.</p>
1235<h5><code>urlObject.slashes</code><span><a class="mark" href="#url_urlobject_slashes" id="url_urlobject_slashes">#</a></span></h5>
1236<p>The <code>slashes</code> property is a <code>boolean</code> with a value of <code>true</code> if two ASCII
1237forward-slash characters (<code>/</code>) are required following the colon in the
1238<code>protocol</code>.</p>
1239<h4><code>url.format(urlObject)</code><span><a class="mark" href="#url_url_format_urlobject" id="url_url_format_urlobject">#</a></span></h4>
1240<div class="api_metadata">
1241<details class="changelog"><summary>History</summary>
1242<table>
1243<tbody><tr><th>Version</th><th>Changes</th></tr>
1244<tr><td>v14.17.0</td>
1245<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr>
1246<tr><td>v11.0.0</td>
1247<td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p></td></tr>
1248<tr><td>v7.0.0</td>
1249<td><p>URLs with a <code>file:</code> scheme will now always use the correct number of slashes regardless of <code>slashes</code> option. A falsy <code>slashes</code> option with no protocol is now also respected at all times.</p></td></tr>
1250<tr><td>v0.1.25</td>
1251<td><p><span>Added in: v0.1.25</span></p></td></tr>
1252</tbody></table>
1253</details>
1254</div>
1255<p></p><div class="api_stability api_stability_3"><a href="documentation.html#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHATWG URL API instead.</div><p></p>
1256<ul>
1257<li><code>urlObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> A URL object (as returned by <code>url.parse()</code> or
1258constructed otherwise). If a string, it is converted to an object by passing
1259it to <code>url.parse()</code>.</li>
1260</ul>
1261<p>The <code>url.format()</code> method returns a formatted URL string derived from
1262<code>urlObject</code>.</p>
1263<pre><code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'url'</span>);
1264url.<span class="hljs-title function_">format</span>({
1265  <span class="hljs-attr">protocol</span>: <span class="hljs-string">'https'</span>,
1266  <span class="hljs-attr">hostname</span>: <span class="hljs-string">'example.com'</span>,
1267  <span class="hljs-attr">pathname</span>: <span class="hljs-string">'/some/path'</span>,
1268  <span class="hljs-attr">query</span>: {
1269    <span class="hljs-attr">page</span>: <span class="hljs-number">1</span>,
1270    <span class="hljs-attr">format</span>: <span class="hljs-string">'json'</span>
1271  }
1272});
1273
1274<span class="hljs-comment">// => 'https://example.com/some/path?page=1&#x26;format=json'</span></code></pre>
1275<p>If <code>urlObject</code> is not an object or a string, <code>url.format()</code> will throw a
1276<a href="errors.html#errors_class_typeerror"><code>TypeError</code></a>.</p>
1277<p>The formatting process operates as follows:</p>
1278<ul>
1279<li>A new empty string <code>result</code> is created.</li>
1280<li>If <code>urlObject.protocol</code> is a string, it is appended as-is to <code>result</code>.</li>
1281<li>Otherwise, if <code>urlObject.protocol</code> is not <code>undefined</code> and is not a string, an
1282<a href="errors.html#errors_class_error"><code>Error</code></a> is thrown.</li>
1283<li>For all string values of <code>urlObject.protocol</code> that <em>do not end</em> with an ASCII
1284colon (<code>:</code>) character, the literal string <code>:</code> will be appended to <code>result</code>.</li>
1285<li>If either of the following conditions is true, then the literal string <code>//</code>
1286will be appended to <code>result</code>:
1287<ul>
1288<li><code>urlObject.slashes</code> property is true;</li>
1289<li><code>urlObject.protocol</code> begins with <code>http</code>, <code>https</code>, <code>ftp</code>, <code>gopher</code>, or
1290<code>file</code>;</li>
1291</ul>
1292</li>
1293<li>If the value of the <code>urlObject.auth</code> property is truthy, and either
1294<code>urlObject.host</code> or <code>urlObject.hostname</code> are not <code>undefined</code>, the value of
1295<code>urlObject.auth</code> will be coerced into a string and appended to <code>result</code>
1296followed by the literal string <code>@</code>.</li>
1297<li>If the <code>urlObject.host</code> property is <code>undefined</code> then:
1298<ul>
1299<li>If the <code>urlObject.hostname</code> is a string, it is appended to <code>result</code>.</li>
1300<li>Otherwise, if <code>urlObject.hostname</code> is not <code>undefined</code> and is not a string,
1301an <a href="errors.html#errors_class_error"><code>Error</code></a> is thrown.</li>
1302<li>If the <code>urlObject.port</code> property value is truthy, and <code>urlObject.hostname</code>
1303is not <code>undefined</code>:
1304<ul>
1305<li>The literal string <code>:</code> is appended to <code>result</code>, and</li>
1306<li>The value of <code>urlObject.port</code> is coerced to a string and appended to
1307<code>result</code>.</li>
1308</ul>
1309</li>
1310</ul>
1311</li>
1312<li>Otherwise, if the <code>urlObject.host</code> property value is truthy, the value of
1313<code>urlObject.host</code> is coerced to a string and appended to <code>result</code>.</li>
1314<li>If the <code>urlObject.pathname</code> property is a string that is not an empty string:
1315<ul>
1316<li>If the <code>urlObject.pathname</code> <em>does not start</em> with an ASCII forward slash
1317(<code>/</code>), then the literal string <code>'/'</code> is appended to <code>result</code>.</li>
1318<li>The value of <code>urlObject.pathname</code> is appended to <code>result</code>.</li>
1319</ul>
1320</li>
1321<li>Otherwise, if <code>urlObject.pathname</code> is not <code>undefined</code> and is not a string, an
1322<a href="errors.html#errors_class_error"><code>Error</code></a> is thrown.</li>
1323<li>If the <code>urlObject.search</code> property is <code>undefined</code> and if the <code>urlObject.query</code>
1324property is an <code>Object</code>, the literal string <code>?</code> is appended to <code>result</code>
1325followed by the output of calling the <a href="querystring.html"><code>querystring</code></a> module's <code>stringify()</code>
1326method passing the value of <code>urlObject.query</code>.</li>
1327<li>Otherwise, if <code>urlObject.search</code> is a string:
1328<ul>
1329<li>If the value of <code>urlObject.search</code> <em>does not start</em> with the ASCII question
1330mark (<code>?</code>) character, the literal string <code>?</code> is appended to <code>result</code>.</li>
1331<li>The value of <code>urlObject.search</code> is appended to <code>result</code>.</li>
1332</ul>
1333</li>
1334<li>Otherwise, if <code>urlObject.search</code> is not <code>undefined</code> and is not a string, an
1335<a href="errors.html#errors_class_error"><code>Error</code></a> is thrown.</li>
1336<li>If the <code>urlObject.hash</code> property is a string:
1337<ul>
1338<li>If the value of <code>urlObject.hash</code> <em>does not start</em> with the ASCII hash (<code>#</code>)
1339character, the literal string <code>#</code> is appended to <code>result</code>.</li>
1340<li>The value of <code>urlObject.hash</code> is appended to <code>result</code>.</li>
1341</ul>
1342</li>
1343<li>Otherwise, if the <code>urlObject.hash</code> property is not <code>undefined</code> and is not a
1344string, an <a href="errors.html#errors_class_error"><code>Error</code></a> is thrown.</li>
1345<li><code>result</code> is returned.</li>
1346</ul>
1347<h4><code>url.parse(urlString[, parseQueryString[, slashesDenoteHost]])</code><span><a class="mark" href="#url_url_parse_urlstring_parsequerystring_slashesdenotehost" id="url_url_parse_urlstring_parsequerystring_slashesdenotehost">#</a></span></h4>
1348<div class="api_metadata">
1349<details class="changelog"><summary>History</summary>
1350<table>
1351<tbody><tr><th>Version</th><th>Changes</th></tr>
1352<tr><td>v14.17.0</td>
1353<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr>
1354<tr><td>v11.14.0</td>
1355<td><p>The <code>pathname</code> property on the returned URL object is now <code>/</code> when there is no path and the protocol scheme is <code>ws:</code> or <code>wss:</code>.</p></td></tr>
1356<tr><td>v11.0.0</td>
1357<td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p></td></tr>
1358<tr><td>v9.0.0</td>
1359<td><p>The <code>search</code> property on the returned URL object is now <code>null</code> when no query string is present.</p></td></tr>
1360<tr><td>v0.1.25</td>
1361<td><p><span>Added in: v0.1.25</span></p></td></tr>
1362</tbody></table>
1363</details>
1364</div>
1365<p></p><div class="api_stability api_stability_3"><a href="documentation.html#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHATWG URL API instead.</div><p></p>
1366<ul>
1367<li><code>urlString</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The URL string to parse.</li>
1368<li><code>parseQueryString</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, the <code>query</code> property will always
1369be set to an object returned by the <a href="querystring.html"><code>querystring</code></a> module's <code>parse()</code>
1370method. If <code>false</code>, the <code>query</code> property on the returned URL object will be an
1371unparsed, undecoded string. <strong>Default:</strong> <code>false</code>.</li>
1372<li><code>slashesDenoteHost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, the first token after the literal
1373string <code>//</code> and preceding the next <code>/</code> will be interpreted as the <code>host</code>.
1374For instance, given <code>//foo/bar</code>, the result would be
1375<code>{host: 'foo', pathname: '/bar'}</code> rather than <code>{pathname: '//foo/bar'}</code>.
1376<strong>Default:</strong> <code>false</code>.</li>
1377</ul>
1378<p>The <code>url.parse()</code> method takes a URL string, parses it, and returns a URL
1379object.</p>
1380<p>A <code>TypeError</code> is thrown if <code>urlString</code> is not a string.</p>
1381<p>A <code>URIError</code> is thrown if the <code>auth</code> property is present but cannot be decoded.</p>
1382<p>Use of the legacy <code>url.parse()</code> method is discouraged. Users should
1383use the WHATWG <code>URL</code> API. Because the <code>url.parse()</code> method uses a
1384lenient, non-standard algorithm for parsing URL strings, security
1385issues can be introduced. Specifically, issues with <a href="https://hackerone.com/reports/678487">host name spoofing</a> and
1386incorrect handling of usernames and passwords have been identified.</p>
1387<h4><code>url.resolve(from, to)</code><span><a class="mark" href="#url_url_resolve_from_to" id="url_url_resolve_from_to">#</a></span></h4>
1388<div class="api_metadata">
1389<details class="changelog"><summary>History</summary>
1390<table>
1391<tbody><tr><th>Version</th><th>Changes</th></tr>
1392<tr><td>v14.17.0</td>
1393<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr>
1394<tr><td>v11.0.0</td>
1395<td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p></td></tr>
1396<tr><td>v6.6.0</td>
1397<td><p>The <code>auth</code> fields are now kept intact when <code>from</code> and <code>to</code> refer to the same host.</p></td></tr>
1398<tr><td>v6.0.0</td>
1399<td><p>The <code>auth</code> fields is cleared now the <code>to</code> parameter contains a hostname.</p></td></tr>
1400<tr><td>v6.5.0, v4.6.2</td>
1401<td><p>The <code>port</code> field is copied correctly now.</p></td></tr>
1402<tr><td>v0.1.25</td>
1403<td><p><span>Added in: v0.1.25</span></p></td></tr>
1404</tbody></table>
1405</details>
1406</div>
1407<p></p><div class="api_stability api_stability_3"><a href="documentation.html#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHATWG URL API instead.</div><p></p>
1408<ul>
1409<li><code>from</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The Base URL being resolved against.</li>
1410<li><code>to</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The HREF URL being resolved.</li>
1411</ul>
1412<p>The <code>url.resolve()</code> method resolves a target URL relative to a base URL in a
1413manner similar to that of a Web browser resolving an anchor tag HREF.</p>
1414<pre><code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'url'</span>);
1415url.<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'/one/two/three'</span>, <span class="hljs-string">'four'</span>);         <span class="hljs-comment">// '/one/two/four'</span>
1416url.<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'http://example.com/'</span>, <span class="hljs-string">'/one'</span>);    <span class="hljs-comment">// 'http://example.com/one'</span>
1417url.<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'http://example.com/one'</span>, <span class="hljs-string">'/two'</span>); <span class="hljs-comment">// 'http://example.com/two'</span></code></pre>
1418<p>You can achieve the same result using the WHATWG URL API:</p>
1419<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">resolve</span>(<span class="hljs-params"><span class="hljs-keyword">from</span>, to</span>) {
1420  <span class="hljs-keyword">const</span> resolvedUrl = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(to, <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-keyword">from</span>, <span class="hljs-string">'resolve://'</span>));
1421  <span class="hljs-keyword">if</span> (resolvedUrl.<span class="hljs-property">protocol</span> === <span class="hljs-string">'resolve:'</span>) {
1422    <span class="hljs-comment">// `from` is a relative URL.</span>
1423    <span class="hljs-keyword">const</span> { pathname, search, hash } = resolvedUrl;
1424    <span class="hljs-keyword">return</span> pathname + search + hash;
1425  }
1426  <span class="hljs-keyword">return</span> resolvedUrl.<span class="hljs-title function_">toString</span>();
1427}
1428
1429<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'/one/two/three'</span>, <span class="hljs-string">'four'</span>);         <span class="hljs-comment">// '/one/two/four'</span>
1430<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'http://example.com/'</span>, <span class="hljs-string">'/one'</span>);    <span class="hljs-comment">// 'http://example.com/one'</span>
1431<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'http://example.com/one'</span>, <span class="hljs-string">'/two'</span>); <span class="hljs-comment">// 'http://example.com/two'</span></code></pre>
1432<p><a id="whatwg-percent-encoding"></a></p>
1433</section><section><h3>Percent-encoding in URLs<span><a class="mark" href="#url_percent_encoding_in_urls" id="url_percent_encoding_in_urls">#</a></span></h3>
1434<p>URLs are permitted to only contain a certain range of characters. Any character
1435falling outside of that range must be encoded. How such characters are encoded,
1436and which characters to encode depends entirely on where the character is
1437located within the structure of the URL.</p>
1438<h4>Legacy API<span><a class="mark" href="#url_legacy_api" id="url_legacy_api">#</a></span></h4>
1439<p>Within the Legacy API, spaces (<code>' '</code>) and the following characters will be
1440automatically escaped in the properties of URL objects:</p>
1441<pre><code class="language-text">&#x3C; > " ` \r \n \t { } | \ ^ '</code></pre>
1442<p>For example, the ASCII space character (<code>' '</code>) is encoded as <code>%20</code>. The ASCII
1443forward slash (<code>/</code>) character is encoded as <code>%3C</code>.</p>
1444<h4>WHATWG API<span><a class="mark" href="#url_whatwg_api" id="url_whatwg_api">#</a></span></h4>
1445<p>The <a href="https://url.spec.whatwg.org/">WHATWG URL Standard</a> uses a more selective and fine grained approach to
1446selecting encoded characters than that used by the Legacy API.</p>
1447<p>The WHATWG algorithm defines four "percent-encode sets" that describe ranges
1448of characters that must be percent-encoded:</p>
1449<ul>
1450<li>
1451<p>The <em>C0 control percent-encode set</em> includes code points in range U+0000 to
1452U+001F (inclusive) and all code points greater than U+007E.</p>
1453</li>
1454<li>
1455<p>The <em>fragment percent-encode set</em> includes the <em>C0 control percent-encode set</em>
1456and code points U+0020, U+0022, U+003C, U+003E, and U+0060.</p>
1457</li>
1458<li>
1459<p>The <em>path percent-encode set</em> includes the <em>C0 control percent-encode set</em>
1460and code points U+0020, U+0022, U+0023, U+003C, U+003E, U+003F, U+0060,
1461U+007B, and U+007D.</p>
1462</li>
1463<li>
1464<p>The <em>userinfo encode set</em> includes the <em>path percent-encode set</em> and code
1465points U+002F, U+003A, U+003B, U+003D, U+0040, U+005B, U+005C, U+005D,
1466U+005E, and U+007C.</p>
1467</li>
1468</ul>
1469<p>The <em>userinfo percent-encode set</em> is used exclusively for username and
1470passwords encoded within the URL. The <em>path percent-encode set</em> is used for the
1471path of most URLs. The <em>fragment percent-encode set</em> is used for URL fragments.
1472The <em>C0 control percent-encode set</em> is used for host and path under certain
1473specific conditions, in addition to all other cases.</p>
1474<p>When non-ASCII characters appear within a host name, the host name is encoded
1475using the <a href="https://tools.ietf.org/html/rfc5891#section-4.4">Punycode</a> algorithm. Note, however, that a host name <em>may</em> contain
1476<em>both</em> Punycode encoded and percent-encoded characters:</p>
1477<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://%CF%80.example.com/foo'</span>);
1478<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
1479<span class="hljs-comment">// Prints https://xn--1xa.example.com/foo</span>
1480<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">origin</span>);
1481<span class="hljs-comment">// Prints https://xn--1xa.example.com</span></code></pre></section>
1482        <!-- API END -->
1483      </div>
1484    </div>
1485  </div>
1486  <script>
1487    'use strict';
1488    {
1489      const kCustomPreference = 'customDarkTheme';
1490      const userSettings = sessionStorage.getItem(kCustomPreference);
1491      const themeToggleButton = document.getElementById('theme-toggle-btn');
1492      if (userSettings === null && window.matchMedia) {
1493        const mq = window.matchMedia('(prefers-color-scheme: dark)');
1494        if ('onchange' in mq) {
1495          function mqChangeListener(e) {
1496            document.body.classList.toggle('dark-mode', e.matches);
1497          }
1498          mq.addEventListener('change', mqChangeListener);
1499          if (themeToggleButton) {
1500            themeToggleButton.addEventListener('click', function() {
1501              mq.removeEventListener('change', mqChangeListener);
1502            }, { once: true });
1503          }
1504        }
1505        if (mq.matches) {
1506          document.body.classList.add('dark-mode');
1507        }
1508      } else if (userSettings === 'true') {
1509        document.body.classList.add('dark-mode');
1510      }
1511      if (themeToggleButton) {
1512        themeToggleButton.hidden = false;
1513        themeToggleButton.addEventListener('click', function() {
1514          sessionStorage.setItem(
1515            kCustomPreference,
1516            document.body.classList.toggle('dark-mode')
1517          );
1518        });
1519      }
1520    }
1521  </script>
1522</body>
1523</html>
1524