• 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="v18.18.2">
7  <title>URL | Node.js v18.18.2 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  <script async defer src="assets/api.js" type="text/javascript"></script>
13  <style>@media(max-width:478px){.with-32-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:726px){.with-63-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:750px){.with-66-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:606px){.with-48-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:614px){.with-49-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style>
14</head>
15<body class="alt apidoc" id="api-section-url">
16  <div id="content" class="clearfix">
17    <div id="column2" class="interior">
18      <div id="intro" class="interior">
19        <a href="/" title="Go back to the home page">
20          Node.js
21        </a>
22      </div>
23      <ul>
24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
26</ul>
27<hr class="line">
28<ul>
29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
33<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
38<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
39<li><a href="console.html" class="nav-console">Console</a></li>
40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
45<li><a href="dns.html" class="nav-dns">DNS</a></li>
46<li><a href="domain.html" class="nav-domain">Domain</a></li>
47<li><a href="errors.html" class="nav-errors">Errors</a></li>
48<li><a href="events.html" class="nav-events">Events</a></li>
49<li><a href="fs.html" class="nav-fs">File system</a></li>
50<li><a href="globals.html" class="nav-globals">Globals</a></li>
51<li><a href="http.html" class="nav-http">HTTP</a></li>
52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
53<li><a href="https.html" class="nav-https">HTTPS</a></li>
54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
55<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
60<li><a href="net.html" class="nav-net">Net</a></li>
61<li><a href="os.html" class="nav-os">OS</a></li>
62<li><a href="path.html" class="nav-path">Path</a></li>
63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
65<li><a href="process.html" class="nav-process">Process</a></li>
66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
68<li><a href="readline.html" class="nav-readline">Readline</a></li>
69<li><a href="repl.html" class="nav-repl">REPL</a></li>
70<li><a href="report.html" class="nav-report">Report</a></li>
71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
72<li><a href="stream.html" class="nav-stream">Stream</a></li>
73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
74<li><a href="test.html" class="nav-test">Test runner</a></li>
75<li><a href="timers.html" class="nav-timers">Timers</a></li>
76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
78<li><a href="tty.html" class="nav-tty">TTY</a></li>
79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
80<li><a href="url.html" class="nav-url active">URL</a></li>
81<li><a href="util.html" class="nav-util">Utilities</a></li>
82<li><a href="v8.html" class="nav-v8">V8</a></li>
83<li><a href="vm.html" class="nav-vm">VM</a></li>
84<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
89</ul>
90<hr class="line">
91<ul>
92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
93</ul>
94    </div>
95
96    <div id="column1" data-id="url" class="interior">
97      <header class="header">
98        <div class="header-container">
99          <h1>Node.js v18.18.2 documentation</h1>
100          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
101            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
102              <path fill="none" d="M0 0h24v24H0z" />
103              <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"/>
104              <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"/>
105            </svg>
106            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
107              <path d="M0 0h24v24H0z" fill="none" />
108              <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"/>
109            </svg>
110          </button>
111        </div>
112        <div id="gtoc">
113          <ul>
114            <li class="pinned-header">Node.js v18.18.2</li>
115
116    <li class="picker-header">
117      <a href="#">
118        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
119        Table of contents
120      </a>
121
122      <div class="picker"><div class="toc"><ul>
123<li><span class="stability_2"><a href="#url">URL</a></span>
124<ul>
125<li><a href="#url-strings-and-url-objects">URL strings and URL objects</a>
126<ul>
127<li><a href="#constructing-a-url-from-component-parts-and-getting-the-constructed-string">Constructing a URL from component parts and getting the constructed string</a></li>
128</ul>
129</li>
130<li><a href="#the-whatwg-url-api">The WHATWG URL API</a>
131<ul>
132<li><a href="#class-url">Class: <code>URL</code></a>
133<ul>
134<li><a href="#new-urlinput-base"><code>new URL(input[, base])</code></a></li>
135<li><a href="#urlhash"><code>url.hash</code></a></li>
136<li><a href="#urlhost"><code>url.host</code></a></li>
137<li><a href="#urlhostname"><code>url.hostname</code></a></li>
138<li><a href="#urlhref"><code>url.href</code></a></li>
139<li><a href="#urlorigin"><code>url.origin</code></a></li>
140<li><a href="#urlpassword"><code>url.password</code></a></li>
141<li><a href="#urlpathname"><code>url.pathname</code></a></li>
142<li><a href="#urlport"><code>url.port</code></a></li>
143<li><a href="#urlprotocol"><code>url.protocol</code></a>
144<ul>
145<li><a href="#special-schemes">Special schemes</a></li>
146</ul>
147</li>
148<li><a href="#urlsearch"><code>url.search</code></a></li>
149<li><a href="#urlsearchparams"><code>url.searchParams</code></a></li>
150<li><a href="#urlusername"><code>url.username</code></a></li>
151<li><a href="#urltostring"><code>url.toString()</code></a></li>
152<li><a href="#urltojson"><code>url.toJSON()</code></a></li>
153<li><span class="stability_1"><a href="#urlcreateobjecturlblob"><code>URL.createObjectURL(blob)</code></a></span></li>
154<li><span class="stability_1"><a href="#urlrevokeobjecturlid"><code>URL.revokeObjectURL(id)</code></a></span></li>
155<li><a href="#urlcanparseinput-base"><code>URL.canParse(input[, base])</code></a></li>
156</ul>
157</li>
158<li><a href="#class-urlsearchparams">Class: <code>URLSearchParams</code></a>
159<ul>
160<li><a href="#new-urlsearchparams"><code>new URLSearchParams()</code></a></li>
161<li><a href="#new-urlsearchparamsstring"><code>new URLSearchParams(string)</code></a></li>
162<li><a href="#new-urlsearchparamsobj"><code>new URLSearchParams(obj)</code></a></li>
163<li><a href="#new-urlsearchparamsiterable"><code>new URLSearchParams(iterable)</code></a></li>
164<li><a href="#urlsearchparamsappendname-value"><code>urlSearchParams.append(name, value)</code></a></li>
165<li><a href="#urlsearchparamsdeletename-value"><code>urlSearchParams.delete(name[, value])</code></a></li>
166<li><a href="#urlsearchparamsentries"><code>urlSearchParams.entries()</code></a></li>
167<li><a href="#urlsearchparamsforeachfn-thisarg"><code>urlSearchParams.forEach(fn[, thisArg])</code></a></li>
168<li><a href="#urlsearchparamsgetname"><code>urlSearchParams.get(name)</code></a></li>
169<li><a href="#urlsearchparamsgetallname"><code>urlSearchParams.getAll(name)</code></a></li>
170<li><a href="#urlsearchparamshasname-value"><code>urlSearchParams.has(name[, value])</code></a></li>
171<li><a href="#urlsearchparamskeys"><code>urlSearchParams.keys()</code></a></li>
172<li><a href="#urlsearchparamssetname-value"><code>urlSearchParams.set(name, value)</code></a></li>
173<li><a href="#urlsearchparamssize"><code>urlSearchParams.size</code></a></li>
174<li><a href="#urlsearchparamssort"><code>urlSearchParams.sort()</code></a></li>
175<li><a href="#urlsearchparamstostring"><code>urlSearchParams.toString()</code></a></li>
176<li><a href="#urlsearchparamsvalues"><code>urlSearchParams.values()</code></a></li>
177<li><a href="#urlsearchparamssymboliterator"><code>urlSearchParams[Symbol.iterator]()</code></a></li>
178</ul>
179</li>
180<li><a href="#urldomaintoasciidomain"><code>url.domainToASCII(domain)</code></a></li>
181<li><a href="#urldomaintounicodedomain"><code>url.domainToUnicode(domain)</code></a></li>
182<li><a href="#urlfileurltopathurl"><code>url.fileURLToPath(url)</code></a></li>
183<li><a href="#urlformaturl-options"><code>url.format(URL[, options])</code></a></li>
184<li><a href="#urlpathtofileurlpath"><code>url.pathToFileURL(path)</code></a></li>
185<li><a href="#urlurltohttpoptionsurl"><code>url.urlToHttpOptions(url)</code></a></li>
186</ul>
187</li>
188<li><span class="stability_3"><a href="#legacy-url-api">Legacy URL API</a></span>
189<ul>
190<li><span class="stability_3"><a href="#legacy-urlobject">Legacy <code>urlObject</code></a></span>
191<ul>
192<li><a href="#urlobjectauth"><code>urlObject.auth</code></a></li>
193<li><a href="#urlobjecthash"><code>urlObject.hash</code></a></li>
194<li><a href="#urlobjecthost"><code>urlObject.host</code></a></li>
195<li><a href="#urlobjecthostname"><code>urlObject.hostname</code></a></li>
196<li><a href="#urlobjecthref"><code>urlObject.href</code></a></li>
197<li><a href="#urlobjectpath"><code>urlObject.path</code></a></li>
198<li><a href="#urlobjectpathname"><code>urlObject.pathname</code></a></li>
199<li><a href="#urlobjectport"><code>urlObject.port</code></a></li>
200<li><a href="#urlobjectprotocol"><code>urlObject.protocol</code></a></li>
201<li><a href="#urlobjectquery"><code>urlObject.query</code></a></li>
202<li><a href="#urlobjectsearch"><code>urlObject.search</code></a></li>
203<li><a href="#urlobjectslashes"><code>urlObject.slashes</code></a></li>
204</ul>
205</li>
206<li><span class="stability_3"><a href="#urlformaturlobject"><code>url.format(urlObject)</code></a></span></li>
207<li><span class="stability_0"><a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse(urlString[, parseQueryString[, slashesDenoteHost]])</code></a></span></li>
208<li><span class="stability_3"><a href="#urlresolvefrom-to"><code>url.resolve(from, to)</code></a></span></li>
209</ul>
210</li>
211<li><a href="#percent-encoding-in-urls">Percent-encoding in URLs</a>
212<ul>
213<li><a href="#legacy-api">Legacy API</a></li>
214<li><a href="#whatwg-api">WHATWG API</a></li>
215</ul>
216</li>
217</ul>
218</li>
219</ul></div></div>
220    </li>
221
222
223    <li class="picker-header">
224      <a href="#">
225        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
226        Index
227      </a>
228
229      <div class="picker"><ul>
230<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
231<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
232
233      <li>
234        <a href="index.html">Index</a>
235      </li>
236    </ul>
237
238<hr class="line">
239<ul>
240<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
241<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
242<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
243<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
244<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
245<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
246<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
247<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
248<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
249<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
250<li><a href="console.html" class="nav-console">Console</a></li>
251<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
252<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
253<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
254<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
255<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
256<li><a href="dns.html" class="nav-dns">DNS</a></li>
257<li><a href="domain.html" class="nav-domain">Domain</a></li>
258<li><a href="errors.html" class="nav-errors">Errors</a></li>
259<li><a href="events.html" class="nav-events">Events</a></li>
260<li><a href="fs.html" class="nav-fs">File system</a></li>
261<li><a href="globals.html" class="nav-globals">Globals</a></li>
262<li><a href="http.html" class="nav-http">HTTP</a></li>
263<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
264<li><a href="https.html" class="nav-https">HTTPS</a></li>
265<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
266<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
267<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
268<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
269<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
270<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
271<li><a href="net.html" class="nav-net">Net</a></li>
272<li><a href="os.html" class="nav-os">OS</a></li>
273<li><a href="path.html" class="nav-path">Path</a></li>
274<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
275<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
276<li><a href="process.html" class="nav-process">Process</a></li>
277<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
278<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
279<li><a href="readline.html" class="nav-readline">Readline</a></li>
280<li><a href="repl.html" class="nav-repl">REPL</a></li>
281<li><a href="report.html" class="nav-report">Report</a></li>
282<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
283<li><a href="stream.html" class="nav-stream">Stream</a></li>
284<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
285<li><a href="test.html" class="nav-test">Test runner</a></li>
286<li><a href="timers.html" class="nav-timers">Timers</a></li>
287<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
288<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
289<li><a href="tty.html" class="nav-tty">TTY</a></li>
290<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
291<li><a href="url.html" class="nav-url active">URL</a></li>
292<li><a href="util.html" class="nav-util">Utilities</a></li>
293<li><a href="v8.html" class="nav-v8">V8</a></li>
294<li><a href="vm.html" class="nav-vm">VM</a></li>
295<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
296<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
297<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
298<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
299<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
300</ul>
301<hr class="line">
302<ul>
303<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
304</ul></div>
305    </li>
306
307
308    <li class="picker-header">
309      <a href="#">
310        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
311        Other versions
312      </a>
313      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v20.x/api/url.html">20.x</a></li>
314<li><a href="https://nodejs.org/docs/latest-v19.x/api/url.html">19.x</a></li>
315<li><a href="https://nodejs.org/docs/latest-v18.x/api/url.html">18.x <b>LTS</b></a></li>
316<li><a href="https://nodejs.org/docs/latest-v17.x/api/url.html">17.x</a></li>
317<li><a href="https://nodejs.org/docs/latest-v16.x/api/url.html">16.x</a></li>
318<li><a href="https://nodejs.org/docs/latest-v15.x/api/url.html">15.x</a></li>
319<li><a href="https://nodejs.org/docs/latest-v14.x/api/url.html">14.x</a></li>
320<li><a href="https://nodejs.org/docs/latest-v13.x/api/url.html">13.x</a></li>
321<li><a href="https://nodejs.org/docs/latest-v12.x/api/url.html">12.x</a></li>
322<li><a href="https://nodejs.org/docs/latest-v11.x/api/url.html">11.x</a></li>
323<li><a href="https://nodejs.org/docs/latest-v10.x/api/url.html">10.x</a></li>
324<li><a href="https://nodejs.org/docs/latest-v9.x/api/url.html">9.x</a></li>
325<li><a href="https://nodejs.org/docs/latest-v8.x/api/url.html">8.x</a></li>
326<li><a href="https://nodejs.org/docs/latest-v7.x/api/url.html">7.x</a></li>
327<li><a href="https://nodejs.org/docs/latest-v6.x/api/url.html">6.x</a></li>
328<li><a href="https://nodejs.org/docs/latest-v5.x/api/url.html">5.x</a></li>
329<li><a href="https://nodejs.org/docs/latest-v4.x/api/url.html">4.x</a></li>
330<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/url.html">0.12.x</a></li>
331<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/url.html">0.10.x</a></li></ol></div>
332    </li>
333
334            <li class="picker-header">
335              <a href="#">
336                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
337                Options
338              </a>
339
340              <div class="picker">
341                <ul>
342                  <li>
343                    <a href="all.html">View on single page</a>
344                  </li>
345                  <li>
346                    <a href="url.json">View as JSON</a>
347                  </li>
348                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/url.md">Edit on GitHub</a></li>
349                </ul>
350              </div>
351            </li>
352          </ul>
353        </div>
354        <hr>
355      </header>
356
357      <details id="toc" open><summary>Table of contents</summary><ul>
358<li><span class="stability_2"><a href="#url">URL</a></span>
359<ul>
360<li><a href="#url-strings-and-url-objects">URL strings and URL objects</a>
361<ul>
362<li><a href="#constructing-a-url-from-component-parts-and-getting-the-constructed-string">Constructing a URL from component parts and getting the constructed string</a></li>
363</ul>
364</li>
365<li><a href="#the-whatwg-url-api">The WHATWG URL API</a>
366<ul>
367<li><a href="#class-url">Class: <code>URL</code></a>
368<ul>
369<li><a href="#new-urlinput-base"><code>new URL(input[, base])</code></a></li>
370<li><a href="#urlhash"><code>url.hash</code></a></li>
371<li><a href="#urlhost"><code>url.host</code></a></li>
372<li><a href="#urlhostname"><code>url.hostname</code></a></li>
373<li><a href="#urlhref"><code>url.href</code></a></li>
374<li><a href="#urlorigin"><code>url.origin</code></a></li>
375<li><a href="#urlpassword"><code>url.password</code></a></li>
376<li><a href="#urlpathname"><code>url.pathname</code></a></li>
377<li><a href="#urlport"><code>url.port</code></a></li>
378<li><a href="#urlprotocol"><code>url.protocol</code></a>
379<ul>
380<li><a href="#special-schemes">Special schemes</a></li>
381</ul>
382</li>
383<li><a href="#urlsearch"><code>url.search</code></a></li>
384<li><a href="#urlsearchparams"><code>url.searchParams</code></a></li>
385<li><a href="#urlusername"><code>url.username</code></a></li>
386<li><a href="#urltostring"><code>url.toString()</code></a></li>
387<li><a href="#urltojson"><code>url.toJSON()</code></a></li>
388<li><span class="stability_1"><a href="#urlcreateobjecturlblob"><code>URL.createObjectURL(blob)</code></a></span></li>
389<li><span class="stability_1"><a href="#urlrevokeobjecturlid"><code>URL.revokeObjectURL(id)</code></a></span></li>
390<li><a href="#urlcanparseinput-base"><code>URL.canParse(input[, base])</code></a></li>
391</ul>
392</li>
393<li><a href="#class-urlsearchparams">Class: <code>URLSearchParams</code></a>
394<ul>
395<li><a href="#new-urlsearchparams"><code>new URLSearchParams()</code></a></li>
396<li><a href="#new-urlsearchparamsstring"><code>new URLSearchParams(string)</code></a></li>
397<li><a href="#new-urlsearchparamsobj"><code>new URLSearchParams(obj)</code></a></li>
398<li><a href="#new-urlsearchparamsiterable"><code>new URLSearchParams(iterable)</code></a></li>
399<li><a href="#urlsearchparamsappendname-value"><code>urlSearchParams.append(name, value)</code></a></li>
400<li><a href="#urlsearchparamsdeletename-value"><code>urlSearchParams.delete(name[, value])</code></a></li>
401<li><a href="#urlsearchparamsentries"><code>urlSearchParams.entries()</code></a></li>
402<li><a href="#urlsearchparamsforeachfn-thisarg"><code>urlSearchParams.forEach(fn[, thisArg])</code></a></li>
403<li><a href="#urlsearchparamsgetname"><code>urlSearchParams.get(name)</code></a></li>
404<li><a href="#urlsearchparamsgetallname"><code>urlSearchParams.getAll(name)</code></a></li>
405<li><a href="#urlsearchparamshasname-value"><code>urlSearchParams.has(name[, value])</code></a></li>
406<li><a href="#urlsearchparamskeys"><code>urlSearchParams.keys()</code></a></li>
407<li><a href="#urlsearchparamssetname-value"><code>urlSearchParams.set(name, value)</code></a></li>
408<li><a href="#urlsearchparamssize"><code>urlSearchParams.size</code></a></li>
409<li><a href="#urlsearchparamssort"><code>urlSearchParams.sort()</code></a></li>
410<li><a href="#urlsearchparamstostring"><code>urlSearchParams.toString()</code></a></li>
411<li><a href="#urlsearchparamsvalues"><code>urlSearchParams.values()</code></a></li>
412<li><a href="#urlsearchparamssymboliterator"><code>urlSearchParams[Symbol.iterator]()</code></a></li>
413</ul>
414</li>
415<li><a href="#urldomaintoasciidomain"><code>url.domainToASCII(domain)</code></a></li>
416<li><a href="#urldomaintounicodedomain"><code>url.domainToUnicode(domain)</code></a></li>
417<li><a href="#urlfileurltopathurl"><code>url.fileURLToPath(url)</code></a></li>
418<li><a href="#urlformaturl-options"><code>url.format(URL[, options])</code></a></li>
419<li><a href="#urlpathtofileurlpath"><code>url.pathToFileURL(path)</code></a></li>
420<li><a href="#urlurltohttpoptionsurl"><code>url.urlToHttpOptions(url)</code></a></li>
421</ul>
422</li>
423<li><span class="stability_3"><a href="#legacy-url-api">Legacy URL API</a></span>
424<ul>
425<li><span class="stability_3"><a href="#legacy-urlobject">Legacy <code>urlObject</code></a></span>
426<ul>
427<li><a href="#urlobjectauth"><code>urlObject.auth</code></a></li>
428<li><a href="#urlobjecthash"><code>urlObject.hash</code></a></li>
429<li><a href="#urlobjecthost"><code>urlObject.host</code></a></li>
430<li><a href="#urlobjecthostname"><code>urlObject.hostname</code></a></li>
431<li><a href="#urlobjecthref"><code>urlObject.href</code></a></li>
432<li><a href="#urlobjectpath"><code>urlObject.path</code></a></li>
433<li><a href="#urlobjectpathname"><code>urlObject.pathname</code></a></li>
434<li><a href="#urlobjectport"><code>urlObject.port</code></a></li>
435<li><a href="#urlobjectprotocol"><code>urlObject.protocol</code></a></li>
436<li><a href="#urlobjectquery"><code>urlObject.query</code></a></li>
437<li><a href="#urlobjectsearch"><code>urlObject.search</code></a></li>
438<li><a href="#urlobjectslashes"><code>urlObject.slashes</code></a></li>
439</ul>
440</li>
441<li><span class="stability_3"><a href="#urlformaturlobject"><code>url.format(urlObject)</code></a></span></li>
442<li><span class="stability_0"><a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse(urlString[, parseQueryString[, slashesDenoteHost]])</code></a></span></li>
443<li><span class="stability_3"><a href="#urlresolvefrom-to"><code>url.resolve(from, to)</code></a></span></li>
444</ul>
445</li>
446<li><a href="#percent-encoding-in-urls">Percent-encoding in URLs</a>
447<ul>
448<li><a href="#legacy-api">Legacy API</a></li>
449<li><a href="#whatwg-api">WHATWG API</a></li>
450</ul>
451</li>
452</ul>
453</li>
454</ul></details>
455
456      <div id="apicontent">
457        <h2>URL<span><a class="mark" href="#url" id="url">#</a></span><a aria-hidden="true" class="legacy" id="url_url"></a></h2>
458
459<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
460<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.18.2/lib/url.js">lib/url.js</a></p>
461<p>The <code>node:url</code> module provides utilities for URL resolution and parsing. It can
462be accessed using:</p>
463
464<pre class="with-32-chars"><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">'node:url'</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">'node:url'</span>);</code><button class="copy-button">copy</button></pre>
465<section><h3>URL strings and URL objects<span><a class="mark" href="#url-strings-and-url-objects" id="url-strings-and-url-objects">#</a></span><a aria-hidden="true" class="legacy" id="url_url_strings_and_url_objects"></a></h3>
466<p>A URL string is a structured string containing multiple meaningful components.
467When parsed, a URL object is returned containing properties for each of these
468components.</p>
469<p>The <code>node:url</code> module provides two APIs for working with URLs: a legacy API that
470is Node.js specific, and a newer API that implements the same
471<a href="https://url.spec.whatwg.org/">WHATWG URL Standard</a> used by web browsers.</p>
472<p>A comparison between the WHATWG and legacy APIs is provided below. Above the URL
473<code>'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'</code>, properties
474of an object returned by the legacy <code>url.parse()</code> are shown. Below it are
475properties of a WHATWG <code>URL</code> object.</p>
476<p>WHATWG URL's <code>origin</code> property includes <code>protocol</code> and <code>host</code>, but not
477<code>username</code> or <code>password</code>.</p>
478<pre><code class="language-text">┌────────────────────────────────────────────────────────────────────────────────────────────────┐
479│                                              href                                              │
480├──────────┬──┬─────────────────────┬────────────────────────┬───────────────────────────┬───────┤
481│ protocol │  │        auth         │          host          │           path            │ hash  │
482│          │  │                     ├─────────────────┬──────┼──────────┬────────────────┤       │
483│          │  │                     │    hostname     │ port │ pathname │     search     │       │
484│          │  │                     │                 │      │          ├─┬──────────────┤       │
485│          │  │                     │                 │      │          │ │    query     │       │
486"  https:   //    user   :   pass   @ sub.example.com : 8080   /p/a/t/h  ?  query=string   #hash "
487│          │  │          │          │    hostname     │ port │          │                │       │
488│          │  │          │          ├─────────────────┴──────┤          │                │       │
489│ protocol │  │ username │ password │          host          │          │                │       │
490├──────────┴──┼──────────┴──────────┼────────────────────────┤          │                │       │
491│   origin    │                     │         origin         │ pathname │     search     │ hash  │
492├─────────────┴─────────────────────┴────────────────────────┴──────────┴────────────────┴───────┤
493│                                              href                                              │
494└────────────────────────────────────────────────────────────────────────────────────────────────┘
495(All spaces in the "" line should be ignored. They are purely for formatting.)</code> <button class="copy-button">copy</button></pre>
496<p>Parsing the URL string using the WHATWG API:</p>
497<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL =
498  <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> <button class="copy-button">copy</button></pre>
499<p>Parsing the URL string using the legacy API:</p>
500
501<pre class="with-32-chars"><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">'node:url'</span>;
502<span class="hljs-keyword">const</span> myURL =
503  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><code class="language-js cjs"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>);
504<span class="hljs-keyword">const</span> myURL =
505  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><button class="copy-button">copy</button></pre>
506<h4>Constructing a URL from component parts and getting the constructed string<span><a class="mark" href="#constructing-a-url-from-component-parts-and-getting-the-constructed-string" id="constructing-a-url-from-component-parts-and-getting-the-constructed-string">#</a></span><a aria-hidden="true" class="legacy" id="url_constructing_a_url_from_component_parts_and_getting_the_constructed_string"></a></h4>
507<p>It is possible to construct a WHATWG URL from component parts using either the
508property setters or a template literal string:</p>
509<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>);
510myURL.<span class="hljs-property">pathname</span> = <span class="hljs-string">'/a/b/c'</span>;
511myURL.<span class="hljs-property">search</span> = <span class="hljs-string">'?d=e'</span>;
512myURL.<span class="hljs-property">hash</span> = <span class="hljs-string">'#fgh'</span>;</code> <button class="copy-button">copy</button></pre>
513<pre><code class="language-js"><span class="hljs-keyword">const</span> pathname = <span class="hljs-string">'/a/b/c'</span>;
514<span class="hljs-keyword">const</span> search = <span class="hljs-string">'?d=e'</span>;
515<span class="hljs-keyword">const</span> hash = <span class="hljs-string">'#fgh'</span>;
516<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> <button class="copy-button">copy</button></pre>
517<p>To get the constructed URL string, use the <code>href</code> property accessor:</p>
518<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> <button class="copy-button">copy</button></pre>
519</section><section><h3>The WHATWG URL API<span><a class="mark" href="#the-whatwg-url-api" id="the-whatwg-url-api">#</a></span><a aria-hidden="true" class="legacy" id="url_the_whatwg_url_api"></a></h3>
520<h4>Class: <code>URL</code><span><a class="mark" href="#class-url" id="class-url">#</a></span><a aria-hidden="true" class="legacy" id="url_class_url"></a></h4>
521<div class="api_metadata">
522<details class="changelog"><summary>History</summary>
523<table>
524<tbody><tr><th>Version</th><th>Changes</th></tr>
525<tr><td>v10.0.0</td>
526<td><p>The class is now available on the global object.</p></td></tr>
527<tr><td>v7.0.0, v6.13.0</td>
528<td><p><span>Added in: v7.0.0, v6.13.0</span></p></td></tr>
529</tbody></table>
530</details>
531</div>
532<p>Browser-compatible <code>URL</code> class, implemented by following the WHATWG URL
533Standard. <a href="https://url.spec.whatwg.org/#example-url-parsing">Examples of parsed URLs</a> may be found in the Standard itself.
534The <code>URL</code> class is also available on the global object.</p>
535<p>In accordance with browser conventions, all properties of <code>URL</code> objects
536are implemented as getters and setters on the class prototype, rather than as
537data properties on the object itself. Thus, unlike <a href="#legacy-urlobject">legacy <code>urlObject</code></a>s,
538using 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
539return <code>true</code>.</p>
540<h5><code>new URL(input[, base])</code><span><a class="mark" href="#new-urlinput-base" id="new-urlinput-base">#</a></span><a aria-hidden="true" class="legacy" id="url_new_url_input_base"></a></h5>
541<!--
542changes:
543  - version: v18.17.0
544    pr-url: https://github.com/nodejs/node/pull/47339
545    description: ICU requirement is removed.
546-->
547<ul>
548<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>
549is relative, then <code>base</code> is required. If <code>input</code> is absolute, the <code>base</code>
550is ignored. If <code>input</code> is not a string, it is <a href="https://tc39.es/ecma262/#sec-tostring">converted to a string</a> first.</li>
551<li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The base URL to resolve against if the <code>input</code> is not
552absolute. If <code>base</code> is not a string, it is <a href="https://tc39.es/ecma262/#sec-tostring">converted to a string</a> first.</li>
553</ul>
554<p>Creates a new <code>URL</code> object by parsing the <code>input</code> relative to the <code>base</code>. If
555<code>base</code> is passed as a string, it will be parsed equivalent to <code>new URL(base)</code>.</p>
556<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>);
557<span class="hljs-comment">// https://example.org/foo</span></code> <button class="copy-button">copy</button></pre>
558<p>The URL constructor is accessible as a property on the global object.
559It can also be imported from the built-in url module:</p>
560
561<pre class="with-63-chars"><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> { <span class="hljs-variable constant_">URL</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>;
562<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable constant_">URL</span> === globalThis.<span class="hljs-property">URL</span>); <span class="hljs-comment">// Prints 'true'.</span></code><code class="language-js cjs"><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">'node:url'</span>).<span class="hljs-property">URL</span>); <span class="hljs-comment">// Prints 'true'.</span></code><button class="copy-button">copy</button></pre>
563<p>A <code>TypeError</code> will be thrown if the <code>input</code> or <code>base</code> are not valid URLs. Note
564that an effort will be made to coerce the given values into strings. For
565instance:</p>
566<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> });
567<span class="hljs-comment">// https://example.org/</span></code> <button class="copy-button">copy</button></pre>
568<p>Unicode characters appearing within the host name of <code>input</code> will be
569automatically converted to ASCII using the <a href="https://tools.ietf.org/html/rfc5891#section-4.4">Punycode</a> algorithm.</p>
570<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>);
571<span class="hljs-comment">// https://xn--g6w251d/</span></code> <button class="copy-button">copy</button></pre>
572<p>In cases where it is not known in advance if <code>input</code> is an absolute URL
573and a <code>base</code> is provided, it is advised to validate that the <code>origin</code> of
574the <code>URL</code> object is what is expected.</p>
575<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>);
576<span class="hljs-comment">// http://example.com/</span>
577
578myURL = <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>);
579<span class="hljs-comment">// https://example.com/</span>
580
581myURL = <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>);
582<span class="hljs-comment">// foo://Example.com/</span>
583
584myURL = <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>);
585<span class="hljs-comment">// http://example.com/</span>
586
587myURL = <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>);
588<span class="hljs-comment">// https://example.org/Example.com/</span>
589
590myURL = <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>);
591<span class="hljs-comment">// foo:Example.com/</span></code> <button class="copy-button">copy</button></pre>
592<h5><code>url.hash</code><span><a class="mark" href="#urlhash" id="urlhash">#</a></span><a aria-hidden="true" class="legacy" id="url_url_hash"></a></h5>
593<ul>
594<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
595</ul>
596<p>Gets and sets the fragment portion of the URL.</p>
597<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>);
598<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">hash</span>);
599<span class="hljs-comment">// Prints #bar</span>
600
601myURL.<span class="hljs-property">hash</span> = <span class="hljs-string">'baz'</span>;
602<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
603<span class="hljs-comment">// Prints https://example.org/foo#baz</span></code> <button class="copy-button">copy</button></pre>
604<p>Invalid URL characters included in the value assigned to the <code>hash</code> property
605are <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which characters to
606percent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a> and
607<a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p>
608<h5><code>url.host</code><span><a class="mark" href="#urlhost" id="urlhost">#</a></span><a aria-hidden="true" class="legacy" id="url_url_host"></a></h5>
609<ul>
610<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
611</ul>
612<p>Gets and sets the host portion of the URL.</p>
613<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>);
614<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">host</span>);
615<span class="hljs-comment">// Prints example.org:81</span>
616
617myURL.<span class="hljs-property">host</span> = <span class="hljs-string">'example.com:82'</span>;
618<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
619<span class="hljs-comment">// Prints https://example.com:82/foo</span></code> <button class="copy-button">copy</button></pre>
620<p>Invalid host values assigned to the <code>host</code> property are ignored.</p>
621<h5><code>url.hostname</code><span><a class="mark" href="#urlhostname" id="urlhostname">#</a></span><a aria-hidden="true" class="legacy" id="url_url_hostname"></a></h5>
622<ul>
623<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
624</ul>
625<p>Gets and sets the host name portion of the URL. The key difference between
626<code>url.host</code> and <code>url.hostname</code> is that <code>url.hostname</code> does <em>not</em> include the
627port.</p>
628<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>);
629<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">hostname</span>);
630<span class="hljs-comment">// Prints example.org</span>
631
632<span class="hljs-comment">// Setting the hostname does not change the port</span>
633myURL.<span class="hljs-property">hostname</span> = <span class="hljs-string">'example.com'</span>;
634<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
635<span class="hljs-comment">// Prints https://example.com:81/foo</span>
636
637<span class="hljs-comment">// Use myURL.host to change the hostname and port</span>
638myURL.<span class="hljs-property">host</span> = <span class="hljs-string">'example.org:82'</span>;
639<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
640<span class="hljs-comment">// Prints https://example.org:82/foo</span></code> <button class="copy-button">copy</button></pre>
641<p>Invalid host name values assigned to the <code>hostname</code> property are ignored.</p>
642<h5><code>url.href</code><span><a class="mark" href="#urlhref" id="urlhref">#</a></span><a aria-hidden="true" class="legacy" id="url_url_href"></a></h5>
643<ul>
644<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
645</ul>
646<p>Gets and sets the serialized URL.</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/foo'</span>);
648<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
649<span class="hljs-comment">// Prints https://example.org/foo</span>
650
651myURL.<span class="hljs-property">href</span> = <span class="hljs-string">'https://example.com/bar'</span>;
652<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
653<span class="hljs-comment">// Prints https://example.com/bar</span></code> <button class="copy-button">copy</button></pre>
654<p>Getting the value of the <code>href</code> property is equivalent to calling
655<a href="#urltostring"><code>url.toString()</code></a>.</p>
656<p>Setting the value of this property to a new value is equivalent to creating a
657new <code>URL</code> object using <a href="#new-urlinput-base"><code>new URL(value)</code></a>. Each of the <code>URL</code>
658object's properties will be modified.</p>
659<p>If the value assigned to the <code>href</code> property is not a valid URL, a <code>TypeError</code>
660will be thrown.</p>
661<h5><code>url.origin</code><span><a class="mark" href="#urlorigin" id="urlorigin">#</a></span><a aria-hidden="true" class="legacy" id="url_url_origin"></a></h5>
662<div class="api_metadata">
663<details class="changelog"><summary>History</summary>
664<table>
665<tbody><tr><th>Version</th><th>Changes</th></tr>
666<tr><td>v15.0.0</td>
667<td><p>The scheme "gopher" is no longer special and <code>url.origin</code> now returns <code>'null'</code> for it.</p></td></tr>
668</tbody></table>
669</details>
670</div>
671<ul>
672<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
673</ul>
674<p>Gets the read-only serialization of the URL's origin.</p>
675<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>);
676<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">origin</span>);
677<span class="hljs-comment">// Prints https://example.org</span></code> <button class="copy-button">copy</button></pre>
678<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>);
679<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(idnURL.<span class="hljs-property">origin</span>);
680<span class="hljs-comment">// Prints https://xn--g6w251d</span>
681
682<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(idnURL.<span class="hljs-property">hostname</span>);
683<span class="hljs-comment">// Prints xn--g6w251d</span></code> <button class="copy-button">copy</button></pre>
684<h5><code>url.password</code><span><a class="mark" href="#urlpassword" id="urlpassword">#</a></span><a aria-hidden="true" class="legacy" id="url_url_password"></a></h5>
685<ul>
686<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
687</ul>
688<p>Gets and sets the password portion of the URL.</p>
689<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>);
690<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">password</span>);
691<span class="hljs-comment">// Prints xyz</span>
692
693myURL.<span class="hljs-property">password</span> = <span class="hljs-string">'123'</span>;
694<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
695<span class="hljs-comment">// Prints https://abc:123@example.com/</span></code> <button class="copy-button">copy</button></pre>
696<p>Invalid URL characters included in the value assigned to the <code>password</code> property
697are <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which characters to
698percent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a> and
699<a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p>
700<h5><code>url.pathname</code><span><a class="mark" href="#urlpathname" id="urlpathname">#</a></span><a aria-hidden="true" class="legacy" id="url_url_pathname"></a></h5>
701<ul>
702<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
703</ul>
704<p>Gets and sets the path portion of the URL.</p>
705<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>);
706<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">pathname</span>);
707<span class="hljs-comment">// Prints /abc/xyz</span>
708
709myURL.<span class="hljs-property">pathname</span> = <span class="hljs-string">'/abcdef'</span>;
710<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
711<span class="hljs-comment">// Prints https://example.org/abcdef?123</span></code> <button class="copy-button">copy</button></pre>
712<p>Invalid URL characters included in the value assigned to the <code>pathname</code>
713property are <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which characters
714to percent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a> and
715<a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p>
716<h5><code>url.port</code><span><a class="mark" href="#urlport" id="urlport">#</a></span><a aria-hidden="true" class="legacy" id="url_url_port"></a></h5>
717<div class="api_metadata">
718<details class="changelog"><summary>History</summary>
719<table>
720<tbody><tr><th>Version</th><th>Changes</th></tr>
721<tr><td>v15.0.0</td>
722<td><p>The scheme "gopher" is no longer special.</p></td></tr>
723</tbody></table>
724</details>
725</div>
726<ul>
727<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
728</ul>
729<p>Gets and sets the port portion of the URL.</p>
730<p>The port value may be a number or a string containing a number in the range
731<code>0</code> to <code>65535</code> (inclusive). Setting the value to the default port of the
732<code>URL</code> objects given <code>protocol</code> will result in the <code>port</code> value becoming
733the empty string (<code>''</code>).</p>
734<p>The port value can be an empty string in which case the port depends on
735the protocol/scheme:</p>
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769<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>"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>
770<p>Upon assigning a value to the port, the value will first be converted to a
771string using <code>.toString()</code>.</p>
772<p>If that string is invalid but it begins with a number, the leading number is
773assigned to <code>port</code>.
774If the number lies outside the range denoted above, it is ignored.</p>
775<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>);
776<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
777<span class="hljs-comment">// Prints 8888</span>
778
779<span class="hljs-comment">// Default ports are automatically transformed to the empty string</span>
780<span class="hljs-comment">// (HTTPS protocol's default port is 443)</span>
781myURL.<span class="hljs-property">port</span> = <span class="hljs-string">'443'</span>;
782<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
783<span class="hljs-comment">// Prints the empty string</span>
784<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
785<span class="hljs-comment">// Prints https://example.org/</span>
786
787myURL.<span class="hljs-property">port</span> = <span class="hljs-number">1234</span>;
788<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
789<span class="hljs-comment">// Prints 1234</span>
790<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
791<span class="hljs-comment">// Prints https://example.org:1234/</span>
792
793<span class="hljs-comment">// Completely invalid port strings are ignored</span>
794myURL.<span class="hljs-property">port</span> = <span class="hljs-string">'abcd'</span>;
795<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
796<span class="hljs-comment">// Prints 1234</span>
797
798<span class="hljs-comment">// Leading numbers are treated as a port number</span>
799myURL.<span class="hljs-property">port</span> = <span class="hljs-string">'5678abcd'</span>;
800<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
801<span class="hljs-comment">// Prints 5678</span>
802
803<span class="hljs-comment">// Non-integers are truncated</span>
804myURL.<span class="hljs-property">port</span> = <span class="hljs-number">1234.5678</span>;
805<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
806<span class="hljs-comment">// Prints 1234</span>
807
808<span class="hljs-comment">// Out-of-range numbers which are not represented in scientific notation</span>
809<span class="hljs-comment">// will be ignored.</span>
810myURL.<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>
811<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
812<span class="hljs-comment">// Prints 1234</span></code> <button class="copy-button">copy</button></pre>
813<p>Numbers which contain a decimal point,
814such as floating-point numbers or numbers in scientific notation,
815are not an exception to this rule.
816Leading numbers up to the decimal point will be set as the URL's port,
817assuming they are valid:</p>
818<pre><code class="language-js">myURL.<span class="hljs-property">port</span> = <span class="hljs-number">4.567e21</span>;
819<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
820<span class="hljs-comment">// Prints 4 (because it is the leading number in the string '4.567e21')</span></code> <button class="copy-button">copy</button></pre>
821<h5><code>url.protocol</code><span><a class="mark" href="#urlprotocol" id="urlprotocol">#</a></span><a aria-hidden="true" class="legacy" id="url_url_protocol"></a></h5>
822<ul>
823<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
824</ul>
825<p>Gets and sets the protocol portion of the URL.</p>
826<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>);
827<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">protocol</span>);
828<span class="hljs-comment">// Prints https:</span>
829
830myURL.<span class="hljs-property">protocol</span> = <span class="hljs-string">'ftp'</span>;
831<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
832<span class="hljs-comment">// Prints ftp://example.org/</span></code> <button class="copy-button">copy</button></pre>
833<p>Invalid URL protocol values assigned to the <code>protocol</code> property are ignored.</p>
834<h6>Special schemes<span><a class="mark" href="#special-schemes" id="special-schemes">#</a></span><a aria-hidden="true" class="legacy" id="url_special_schemes"></a></h6>
835<div class="api_metadata">
836<details class="changelog"><summary>History</summary>
837<table>
838<tbody><tr><th>Version</th><th>Changes</th></tr>
839<tr><td>v15.0.0</td>
840<td><p>The scheme "gopher" is no longer special.</p></td></tr>
841</tbody></table>
842</details>
843</div>
844<p>The <a href="https://url.spec.whatwg.org/">WHATWG URL Standard</a> considers a handful of URL protocol schemes to be
845<em>special</em> in terms of how they are parsed and serialized. When a URL is
846parsed using one of these special protocols, the <code>url.protocol</code> property
847may be changed to another special protocol but cannot be changed to a
848non-special protocol, and vice versa.</p>
849<p>For instance, changing from <code>http</code> to <code>https</code> works:</p>
850<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>);
851u.<span class="hljs-property">protocol</span> = <span class="hljs-string">'https'</span>;
852<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(u.<span class="hljs-property">href</span>);
853<span class="hljs-comment">// https://example.org/</span></code> <button class="copy-button">copy</button></pre>
854<p>However, changing from <code>http</code> to a hypothetical <code>fish</code> protocol does not
855because the new protocol is not special.</p>
856<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>);
857u.<span class="hljs-property">protocol</span> = <span class="hljs-string">'fish'</span>;
858<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(u.<span class="hljs-property">href</span>);
859<span class="hljs-comment">// http://example.org/</span></code> <button class="copy-button">copy</button></pre>
860<p>Likewise, changing from a non-special protocol to a special protocol is also
861not permitted:</p>
862<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>);
863u.<span class="hljs-property">protocol</span> = <span class="hljs-string">'http'</span>;
864<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(u.<span class="hljs-property">href</span>);
865<span class="hljs-comment">// fish://example.org</span></code> <button class="copy-button">copy</button></pre>
866<p>According to the WHATWG URL Standard, special protocol schemes are <code>ftp</code>,
867<code>file</code>, <code>http</code>, <code>https</code>, <code>ws</code>, and <code>wss</code>.</p>
868<h5><code>url.search</code><span><a class="mark" href="#urlsearch" id="urlsearch">#</a></span><a aria-hidden="true" class="legacy" id="url_url_search"></a></h5>
869<ul>
870<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
871</ul>
872<p>Gets and sets the serialized query portion of the URL.</p>
873<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>);
874<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">search</span>);
875<span class="hljs-comment">// Prints ?123</span>
876
877myURL.<span class="hljs-property">search</span> = <span class="hljs-string">'abc=xyz'</span>;
878<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
879<span class="hljs-comment">// Prints https://example.org/abc?abc=xyz</span></code> <button class="copy-button">copy</button></pre>
880<p>Any invalid URL characters appearing in the value assigned the <code>search</code>
881property will be <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which
882characters to percent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a>
883and <a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p>
884<h5><code>url.searchParams</code><span><a class="mark" href="#urlsearchparams" id="urlsearchparams">#</a></span><a aria-hidden="true" class="legacy" id="url_url_searchparams"></a></h5>
885<ul>
886<li><a href="url.html#class-urlsearchparams" class="type">&#x3C;URLSearchParams></a></li>
887</ul>
888<p>Gets the <a href="#class-urlsearchparams"><code>URLSearchParams</code></a> object representing the query parameters of the
889URL. This property is read-only but the <code>URLSearchParams</code> object it provides
890can be used to mutate the URL instance; to replace the entirety of query
891parameters of the URL, use the <a href="#urlsearch"><code>url.search</code></a> setter. See
892<a href="#class-urlsearchparams"><code>URLSearchParams</code></a> documentation for details.</p>
893<p>Use care when using <code>.searchParams</code> to modify the <code>URL</code> because,
894per the WHATWG specification, the <code>URLSearchParams</code> object uses
895different rules to determine which characters to percent-encode. For
896instance, the <code>URL</code> object will not percent encode the ASCII tilde (<code>~</code>)
897character, while <code>URLSearchParams</code> will always encode it:</p>
898<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>);
899
900<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>
901
902<span class="hljs-comment">// Modify the URL via searchParams...</span>
903myURL.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">sort</span>();
904
905<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> <button class="copy-button">copy</button></pre>
906<h5><code>url.username</code><span><a class="mark" href="#urlusername" id="urlusername">#</a></span><a aria-hidden="true" class="legacy" id="url_url_username"></a></h5>
907<ul>
908<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
909</ul>
910<p>Gets and sets the username portion of the URL.</p>
911<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>);
912<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">username</span>);
913<span class="hljs-comment">// Prints abc</span>
914
915myURL.<span class="hljs-property">username</span> = <span class="hljs-string">'123'</span>;
916<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
917<span class="hljs-comment">// Prints https://123:xyz@example.com/</span></code> <button class="copy-button">copy</button></pre>
918<p>Any invalid URL characters appearing in the value assigned the <code>username</code>
919property will be <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which
920characters to percent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a>
921and <a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p>
922<h5><code>url.toString()</code><span><a class="mark" href="#urltostring" id="urltostring">#</a></span><a aria-hidden="true" class="legacy" id="url_url_tostring"></a></h5>
923<ul>
924<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
925</ul>
926<p>The <code>toString()</code> method on the <code>URL</code> object returns the serialized URL. The
927value returned is equivalent to that of <a href="#urlhref"><code>url.href</code></a> and <a href="#urltojson"><code>url.toJSON()</code></a>.</p>
928<h5><code>url.toJSON()</code><span><a class="mark" href="#urltojson" id="urltojson">#</a></span><a aria-hidden="true" class="legacy" id="url_url_tojson"></a></h5>
929<ul>
930<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
931</ul>
932<p>The <code>toJSON()</code> method on the <code>URL</code> object returns the serialized URL. The
933value returned is equivalent to that of <a href="#urlhref"><code>url.href</code></a> and
934<a href="#urltostring"><code>url.toString()</code></a>.</p>
935<p>This method is automatically called when an <code>URL</code> object is serialized
936with <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify"><code>JSON.stringify()</code></a>.</p>
937<pre><code class="language-js"><span class="hljs-keyword">const</span> myURLs = [
938  <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://www.example.com'</span>),
939  <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://test.example.org'</span>),
940];
941<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(myURLs));
942<span class="hljs-comment">// Prints ["https://www.example.com/","https://test.example.org/"]</span></code> <button class="copy-button">copy</button></pre>
943<h5><code>URL.createObjectURL(blob)</code><span><a class="mark" href="#urlcreateobjecturlblob" id="urlcreateobjecturlblob">#</a></span><a aria-hidden="true" class="legacy" id="url_url_createobjecturl_blob"></a></h5>
944<div class="api_metadata">
945<span>Added in: v16.7.0</span>
946</div>
947<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
948<ul>
949<li><code>blob</code> <a href="buffer.html#class-blob" class="type">&#x3C;Blob></a></li>
950<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
951</ul>
952<p>Creates a <code>'blob:nodedata:...'</code> URL string that represents the given <a href="buffer.html#class-blob" class="type">&#x3C;Blob></a>
953object and can be used to retrieve the <code>Blob</code> later.</p>
954<pre><code class="language-js"><span class="hljs-keyword">const</span> {
955  <span class="hljs-title class_">Blob</span>,
956  resolveObjectURL,
957} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
958
959<span class="hljs-keyword">const</span> blob = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Blob</span>([<span class="hljs-string">'hello'</span>]);
960<span class="hljs-keyword">const</span> id = <span class="hljs-variable constant_">URL</span>.<span class="hljs-title function_">createObjectURL</span>(blob);
961
962<span class="hljs-comment">// later...</span>
963
964<span class="hljs-keyword">const</span> otherBlob = <span class="hljs-title function_">resolveObjectURL</span>(id);
965<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(otherBlob.<span class="hljs-property">size</span>);</code> <button class="copy-button">copy</button></pre>
966<p>The data stored by the registered <a href="buffer.html#class-blob" class="type">&#x3C;Blob></a> will be retained in memory until
967<code>URL.revokeObjectURL()</code> is called to remove it.</p>
968<p><code>Blob</code> objects are registered within the current thread. If using Worker
969Threads, <code>Blob</code> objects registered within one Worker will not be available
970to other workers or the main thread.</p>
971<h5><code>URL.revokeObjectURL(id)</code><span><a class="mark" href="#urlrevokeobjecturlid" id="urlrevokeobjecturlid">#</a></span><a aria-hidden="true" class="legacy" id="url_url_revokeobjecturl_id"></a></h5>
972<div class="api_metadata">
973<span>Added in: v16.7.0</span>
974</div>
975<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
976<ul>
977<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> A <code>'blob:nodedata:...</code> URL string returned by a prior call to
978<code>URL.createObjectURL()</code>.</li>
979</ul>
980<p>Removes the stored <a href="buffer.html#class-blob" class="type">&#x3C;Blob></a> identified by the given ID. Attempting to revoke a
981ID that isn't registered will silently fail.</p>
982<h5><code>URL.canParse(input[, base])</code><span><a class="mark" href="#urlcanparseinput-base" id="urlcanparseinput-base">#</a></span><a aria-hidden="true" class="legacy" id="url_url_canparse_input_base"></a></h5>
983<div class="api_metadata">
984<span>Added in: v18.17.0</span>
985</div>
986<ul>
987<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>
988is relative, then <code>base</code> is required. If <code>input</code> is absolute, the <code>base</code>
989is ignored. If <code>input</code> is not a string, it is <a href="https://tc39.es/ecma262/#sec-tostring">converted to a string</a> first.</li>
990<li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The base URL to resolve against if the <code>input</code> is not
991absolute. If <code>base</code> is not a string, it is <a href="https://tc39.es/ecma262/#sec-tostring">converted to a string</a> first.</li>
992<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
993</ul>
994<p>Checks if an <code>input</code> relative to the <code>base</code> can be parsed to a <code>URL</code>.</p>
995<pre><code class="language-js"><span class="hljs-keyword">const</span> isValid = <span class="hljs-variable constant_">URL</span>.<span class="hljs-title function_">canParse</span>(<span class="hljs-string">'/foo'</span>, <span class="hljs-string">'https://example.org/'</span>); <span class="hljs-comment">// true</span>
996
997<span class="hljs-keyword">const</span> isNotValid = <span class="hljs-variable constant_">URL</span>.<span class="hljs-title function_">canParse</span>(<span class="hljs-string">'/foo'</span>); <span class="hljs-comment">// false</span></code> <button class="copy-button">copy</button></pre>
998<h4>Class: <code>URLSearchParams</code><span><a class="mark" href="#class-urlsearchparams" id="class-urlsearchparams">#</a></span><a aria-hidden="true" class="legacy" id="url_class_urlsearchparams"></a></h4>
999<div class="api_metadata">
1000<details class="changelog"><summary>History</summary>
1001<table>
1002<tbody><tr><th>Version</th><th>Changes</th></tr>
1003<tr><td>v10.0.0</td>
1004<td><p>The class is now available on the global object.</p></td></tr>
1005<tr><td>v7.5.0, v6.13.0</td>
1006<td><p><span>Added in: v7.5.0, v6.13.0</span></p></td></tr>
1007</tbody></table>
1008</details>
1009</div>
1010<p>The <code>URLSearchParams</code> API provides read and write access to the query of a
1011<code>URL</code>. The <code>URLSearchParams</code> class can also be used standalone with one of the
1012four following constructors.
1013The <code>URLSearchParams</code> class is also available on the global object.</p>
1014<p>The WHATWG <code>URLSearchParams</code> interface and the <a href="querystring.html"><code>querystring</code></a> module have
1015similar purpose, but the purpose of the <a href="querystring.html"><code>querystring</code></a> module is more
1016general, as it allows the customization of delimiter characters (<code>&#x26;</code> and <code>=</code>).
1017On the other hand, this API is designed purely for URL query strings.</p>
1018<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>);
1019<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>));
1020<span class="hljs-comment">// Prints 123</span>
1021
1022myURL.<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>);
1023<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
1024<span class="hljs-comment">// Prints https://example.org/?abc=123&#x26;abc=xyz</span>
1025
1026myURL.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">delete</span>(<span class="hljs-string">'abc'</span>);
1027myURL.<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>);
1028<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
1029<span class="hljs-comment">// Prints https://example.org/?a=b</span>
1030
1031<span class="hljs-keyword">const</span> newSearchParams = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(myURL.<span class="hljs-property">searchParams</span>);
1032<span class="hljs-comment">// The above is equivalent to</span>
1033<span class="hljs-comment">// const newSearchParams = new URLSearchParams(myURL.search);</span>
1034
1035newSearchParams.<span class="hljs-title function_">append</span>(<span class="hljs-string">'a'</span>, <span class="hljs-string">'c'</span>);
1036<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
1037<span class="hljs-comment">// Prints https://example.org/?a=b</span>
1038<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(newSearchParams.<span class="hljs-title function_">toString</span>());
1039<span class="hljs-comment">// Prints a=b&#x26;a=c</span>
1040
1041<span class="hljs-comment">// newSearchParams.toString() is implicitly called</span>
1042myURL.<span class="hljs-property">search</span> = newSearchParams;
1043<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
1044<span class="hljs-comment">// Prints https://example.org/?a=b&#x26;a=c</span>
1045newSearchParams.<span class="hljs-title function_">delete</span>(<span class="hljs-string">'a'</span>);
1046<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
1047<span class="hljs-comment">// Prints https://example.org/?a=b&#x26;a=c</span></code> <button class="copy-button">copy</button></pre>
1048<h5><code>new URLSearchParams()</code><span><a class="mark" href="#new-urlsearchparams" id="new-urlsearchparams">#</a></span><a aria-hidden="true" class="legacy" id="url_new_urlsearchparams"></a></h5>
1049<p>Instantiate a new empty <code>URLSearchParams</code> object.</p>
1050<h5><code>new URLSearchParams(string)</code><span><a class="mark" href="#new-urlsearchparamsstring" id="new-urlsearchparamsstring">#</a></span><a aria-hidden="true" class="legacy" id="url_new_urlsearchparams_string"></a></h5>
1051<ul>
1052<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>
1053</ul>
1054<p>Parse the <code>string</code> as a query string, and use it to instantiate a new
1055<code>URLSearchParams</code> object. A leading <code>'?'</code>, if present, is ignored.</p>
1056<pre><code class="language-js"><span class="hljs-keyword">let</span> params;
1057
1058params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(<span class="hljs-string">'user=abc&#x26;query=xyz'</span>);
1059<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>));
1060<span class="hljs-comment">// Prints 'abc'</span>
1061<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
1062<span class="hljs-comment">// Prints 'user=abc&#x26;query=xyz'</span>
1063
1064params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(<span class="hljs-string">'?user=abc&#x26;query=xyz'</span>);
1065<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
1066<span class="hljs-comment">// Prints 'user=abc&#x26;query=xyz'</span></code> <button class="copy-button">copy</button></pre>
1067<h5><code>new URLSearchParams(obj)</code><span><a class="mark" href="#new-urlsearchparamsobj" id="new-urlsearchparamsobj">#</a></span><a aria-hidden="true" class="legacy" id="url_new_urlsearchparams_obj"></a></h5>
1068<div class="api_metadata">
1069<span>Added in: v7.10.0, v6.13.0</span>
1070</div>
1071<ul>
1072<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>
1073</ul>
1074<p>Instantiate a new <code>URLSearchParams</code> object with a query hash map. The key and
1075value of each property of <code>obj</code> are always coerced to strings.</p>
1076<p>Unlike <a href="querystring.html"><code>querystring</code></a> module, duplicate keys in the form of array values are
1077not 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
1078joins all array elements with commas.</p>
1079<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>({
1080  <span class="hljs-attr">user</span>: <span class="hljs-string">'abc'</span>,
1081  <span class="hljs-attr">query</span>: [<span class="hljs-string">'first'</span>, <span class="hljs-string">'second'</span>],
1082});
1083<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>));
1084<span class="hljs-comment">// Prints [ 'first,second' ]</span>
1085<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
1086<span class="hljs-comment">// Prints 'user=abc&#x26;query=first%2Csecond'</span></code> <button class="copy-button">copy</button></pre>
1087<h5><code>new URLSearchParams(iterable)</code><span><a class="mark" href="#new-urlsearchparamsiterable" id="new-urlsearchparamsiterable">#</a></span><a aria-hidden="true" class="legacy" id="url_new_urlsearchparams_iterable"></a></h5>
1088<div class="api_metadata">
1089<span>Added in: v7.10.0, v6.13.0</span>
1090</div>
1091<ul>
1092<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>
1093</ul>
1094<p>Instantiate a new <code>URLSearchParams</code> object with an iterable map in a way that
1095is 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
1096iterable object. That means <code>iterable</code> can be another <code>URLSearchParams</code>, in
1097which case the constructor will simply create a clone of the provided
1098<code>URLSearchParams</code>. Elements of <code>iterable</code> are key-value pairs, and can
1099themselves be any iterable object.</p>
1100<p>Duplicate keys are allowed.</p>
1101<pre><code class="language-js"><span class="hljs-keyword">let</span> params;
1102
1103<span class="hljs-comment">// Using an array</span>
1104params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>([
1105  [<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>],
1106  [<span class="hljs-string">'query'</span>, <span class="hljs-string">'first'</span>],
1107  [<span class="hljs-string">'query'</span>, <span class="hljs-string">'second'</span>],
1108]);
1109<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
1110<span class="hljs-comment">// Prints 'user=abc&#x26;query=first&#x26;query=second'</span>
1111
1112<span class="hljs-comment">// Using a Map object</span>
1113<span class="hljs-keyword">const</span> map = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>();
1114map.<span class="hljs-title function_">set</span>(<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>);
1115map.<span class="hljs-title function_">set</span>(<span class="hljs-string">'query'</span>, <span class="hljs-string">'xyz'</span>);
1116params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(map);
1117<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
1118<span class="hljs-comment">// Prints 'user=abc&#x26;query=xyz'</span>
1119
1120<span class="hljs-comment">// Using a generator function</span>
1121<span class="hljs-keyword">function</span>* <span class="hljs-title function_">getQueryPairs</span>(<span class="hljs-params"></span>) {
1122  <span class="hljs-keyword">yield</span> [<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>];
1123  <span class="hljs-keyword">yield</span> [<span class="hljs-string">'query'</span>, <span class="hljs-string">'first'</span>];
1124  <span class="hljs-keyword">yield</span> [<span class="hljs-string">'query'</span>, <span class="hljs-string">'second'</span>];
1125}
1126params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(<span class="hljs-title function_">getQueryPairs</span>());
1127<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
1128<span class="hljs-comment">// Prints 'user=abc&#x26;query=first&#x26;query=second'</span>
1129
1130<span class="hljs-comment">// Each key-value pair must have exactly two elements</span>
1131<span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>([
1132  [<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>, <span class="hljs-string">'error'</span>],
1133]);
1134<span class="hljs-comment">// Throws TypeError [ERR_INVALID_TUPLE]:</span>
1135<span class="hljs-comment">//        Each query pair must be an iterable [name, value] tuple</span></code> <button class="copy-button">copy</button></pre>
1136<h5><code>urlSearchParams.append(name, value)</code><span><a class="mark" href="#urlsearchparamsappendname-value" id="urlsearchparamsappendname-value">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_append_name_value"></a></h5>
1137<ul>
1138<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>
1139<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>
1140</ul>
1141<p>Append a new name-value pair to the query string.</p>
1142<h5><code>urlSearchParams.delete(name[, value])</code><span><a class="mark" href="#urlsearchparamsdeletename-value" id="urlsearchparamsdeletename-value">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_delete_name_value"></a></h5>
1143<div class="api_metadata">
1144<details class="changelog"><summary>History</summary>
1145<table>
1146<tbody><tr><th>Version</th><th>Changes</th></tr>
1147<tr><td>v18.18.0</td>
1148<td><p>Add support for optional <code>value</code> argument.</p></td></tr>
1149</tbody></table>
1150</details>
1151</div>
1152<ul>
1153<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>
1154<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>
1155</ul>
1156<p>If <code>value</code> is provided, removes all name-value pairs
1157where name is <code>name</code> and value is <code>value</code>..</p>
1158<p>If <code>value</code> is not provided, removes all name-value pairs whose name is <code>name</code>.</p>
1159<h5><code>urlSearchParams.entries()</code><span><a class="mark" href="#urlsearchparamsentries" id="urlsearchparamsentries">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_entries"></a></h5>
1160<ul>
1161<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>
1162</ul>
1163<p>Returns an ES6 <code>Iterator</code> over each of the name-value pairs in the query.
1164Each item of the iterator is a JavaScript <code>Array</code>. The first item of the <code>Array</code>
1165is the <code>name</code>, the second item of the <code>Array</code> is the <code>value</code>.</p>
1166<p>Alias for <a href="#urlsearchparamssymboliterator"><code>urlSearchParams[@@iterator]()</code></a>.</p>
1167<h5><code>urlSearchParams.forEach(fn[, thisArg])</code><span><a class="mark" href="#urlsearchparamsforeachfn-thisarg" id="urlsearchparamsforeachfn-thisarg">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_foreach_fn_thisarg"></a></h5>
1168<div class="api_metadata">
1169<details class="changelog"><summary>History</summary>
1170<table>
1171<tbody><tr><th>Version</th><th>Changes</th></tr>
1172<tr><td>v18.0.0</td>
1173<td><p>Passing an invalid callback to the <code>fn</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
1174</tbody></table>
1175</details>
1176</div>
1177<ul>
1178<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>
1179<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>
1180</ul>
1181<p>Iterates over each name-value pair in the query and invokes the given function.</p>
1182<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>);
1183myURL.<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> {
1184  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name, value, myURL.<span class="hljs-property">searchParams</span> === searchParams);
1185});
1186<span class="hljs-comment">// Prints:</span>
1187<span class="hljs-comment">//   a b true</span>
1188<span class="hljs-comment">//   c d true</span></code> <button class="copy-button">copy</button></pre>
1189<h5><code>urlSearchParams.get(name)</code><span><a class="mark" href="#urlsearchparamsgetname" id="urlsearchparamsgetname">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_get_name"></a></h5>
1190<ul>
1191<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>
1192<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
1193<code>name</code>.</li>
1194</ul>
1195<p>Returns the value of the first name-value pair whose name is <code>name</code>. If there
1196are no such pairs, <code>null</code> is returned.</p>
1197<h5><code>urlSearchParams.getAll(name)</code><span><a class="mark" href="#urlsearchparamsgetallname" id="urlsearchparamsgetallname">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_getall_name"></a></h5>
1198<ul>
1199<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>
1200<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
1201</ul>
1202<p>Returns the values of all name-value pairs whose name is <code>name</code>. If there are
1203no such pairs, an empty array is returned.</p>
1204<h5><code>urlSearchParams.has(name[, value])</code><span><a class="mark" href="#urlsearchparamshasname-value" id="urlsearchparamshasname-value">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_has_name_value"></a></h5>
1205<div class="api_metadata">
1206<details class="changelog"><summary>History</summary>
1207<table>
1208<tbody><tr><th>Version</th><th>Changes</th></tr>
1209<tr><td>v18.18.0</td>
1210<td><p>Add support for optional <code>value</code> argument.</p></td></tr>
1211</tbody></table>
1212</details>
1213</div>
1214<ul>
1215<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>
1216<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>
1217<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1218</ul>
1219<p>Checks if the <code>URLSearchParams</code> object contains key-value pair(s) based on
1220<code>name</code> and an optional <code>value</code> argument.</p>
1221<p>If <code>value</code> is provided, returns <code>true</code> when name-value pair with
1222same <code>name</code> and <code>value</code> exists.</p>
1223<p>If <code>value</code> is not provided, returns <code>true</code> if there is at least one name-value
1224pair whose name is <code>name</code>.</p>
1225<h5><code>urlSearchParams.keys()</code><span><a class="mark" href="#urlsearchparamskeys" id="urlsearchparamskeys">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_keys"></a></h5>
1226<ul>
1227<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>
1228</ul>
1229<p>Returns an ES6 <code>Iterator</code> over the names of each name-value pair.</p>
1230<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(<span class="hljs-string">'foo=bar&#x26;foo=baz'</span>);
1231<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>()) {
1232  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name);
1233}
1234<span class="hljs-comment">// Prints:</span>
1235<span class="hljs-comment">//   foo</span>
1236<span class="hljs-comment">//   foo</span></code> <button class="copy-button">copy</button></pre>
1237<h5><code>urlSearchParams.set(name, value)</code><span><a class="mark" href="#urlsearchparamssetname-value" id="urlsearchparamssetname-value">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_set_name_value"></a></h5>
1238<ul>
1239<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>
1240<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>
1241</ul>
1242<p>Sets the value in the <code>URLSearchParams</code> object associated with <code>name</code> to
1243<code>value</code>. If there are any pre-existing name-value pairs whose names are <code>name</code>,
1244set the first such pair's value to <code>value</code> and remove all others. If not,
1245append the name-value pair to the query string.</p>
1246<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>();
1247params.<span class="hljs-title function_">append</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>);
1248params.<span class="hljs-title function_">append</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'baz'</span>);
1249params.<span class="hljs-title function_">append</span>(<span class="hljs-string">'abc'</span>, <span class="hljs-string">'def'</span>);
1250<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
1251<span class="hljs-comment">// Prints foo=bar&#x26;foo=baz&#x26;abc=def</span>
1252
1253params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'def'</span>);
1254params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'xyz'</span>, <span class="hljs-string">'opq'</span>);
1255<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
1256<span class="hljs-comment">// Prints foo=def&#x26;abc=def&#x26;xyz=opq</span></code> <button class="copy-button">copy</button></pre>
1257<h5><code>urlSearchParams.size</code><span><a class="mark" href="#urlsearchparamssize" id="urlsearchparamssize">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_size"></a></h5>
1258<div class="api_metadata">
1259<span>Added in: v18.16.0</span>
1260</div>
1261<p>The total number of parameter entries.</p>
1262<h5><code>urlSearchParams.sort()</code><span><a class="mark" href="#urlsearchparamssort" id="urlsearchparamssort">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_sort"></a></h5>
1263<div class="api_metadata">
1264<span>Added in: v7.7.0, v6.13.0</span>
1265</div>
1266<p>Sort all existing name-value pairs in-place by their names. Sorting is done
1267with a <a href="https://en.wikipedia.org/wiki/Sorting_algorithm#Stability">stable sorting algorithm</a>, so relative order between name-value pairs
1268with the same name is preserved.</p>
1269<p>This method can be used, in particular, to increase cache hits.</p>
1270<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(<span class="hljs-string">'query[]=abc&#x26;type=search&#x26;query[]=123'</span>);
1271params.<span class="hljs-title function_">sort</span>();
1272<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
1273<span class="hljs-comment">// Prints query%5B%5D=abc&#x26;query%5B%5D=123&#x26;type=search</span></code> <button class="copy-button">copy</button></pre>
1274<h5><code>urlSearchParams.toString()</code><span><a class="mark" href="#urlsearchparamstostring" id="urlsearchparamstostring">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_tostring"></a></h5>
1275<ul>
1276<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1277</ul>
1278<p>Returns the search parameters serialized as a string, with characters
1279percent-encoded where necessary.</p>
1280<h5><code>urlSearchParams.values()</code><span><a class="mark" href="#urlsearchparamsvalues" id="urlsearchparamsvalues">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_values"></a></h5>
1281<ul>
1282<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>
1283</ul>
1284<p>Returns an ES6 <code>Iterator</code> over the values of each name-value pair.</p>
1285<h5><code>urlSearchParams[Symbol.iterator]()</code><span><a class="mark" href="#urlsearchparamssymboliterator" id="urlsearchparamssymboliterator">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_symbol_iterator"></a></h5>
1286<ul>
1287<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>
1288</ul>
1289<p>Returns an ES6 <code>Iterator</code> over each of the name-value pairs in the query string.
1290Each item of the iterator is a JavaScript <code>Array</code>. The first item of the <code>Array</code>
1291is the <code>name</code>, the second item of the <code>Array</code> is the <code>value</code>.</p>
1292<p>Alias for <a href="#urlsearchparamsentries"><code>urlSearchParams.entries()</code></a>.</p>
1293<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(<span class="hljs-string">'foo=bar&#x26;xyz=baz'</span>);
1294<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> [name, value] <span class="hljs-keyword">of</span> params) {
1295  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name, value);
1296}
1297<span class="hljs-comment">// Prints:</span>
1298<span class="hljs-comment">//   foo bar</span>
1299<span class="hljs-comment">//   xyz baz</span></code> <button class="copy-button">copy</button></pre>
1300<h4><code>url.domainToASCII(domain)</code><span><a class="mark" href="#urldomaintoasciidomain" id="urldomaintoasciidomain">#</a></span><a aria-hidden="true" class="legacy" id="url_url_domaintoascii_domain"></a></h4>
1301<div class="api_metadata">
1302<details class="changelog"><summary>History</summary>
1303<table>
1304<tbody><tr><th>Version</th><th>Changes</th></tr>
1305<tr><td>v18.17.0</td>
1306<td><p>ICU requirement is removed.</p></td></tr>
1307<tr><td>v7.4.0, v6.13.0</td>
1308<td><p><span>Added in: v7.4.0, v6.13.0</span></p></td></tr>
1309</tbody></table>
1310</details>
1311</div>
1312<ul>
1313<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>
1314<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1315</ul>
1316<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
1317invalid domain, the empty string is returned.</p>
1318<p>It performs the inverse operation to <a href="#urldomaintounicodedomain"><code>url.domainToUnicode()</code></a>.</p>
1319
1320<pre class="with-32-chars"><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">'node:url'</span>;
1321
1322<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>));
1323<span class="hljs-comment">// Prints xn--espaol-zwa.com</span>
1324<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>));
1325<span class="hljs-comment">// Prints xn--fiq228c.com</span>
1326<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>));
1327<span class="hljs-comment">// Prints an empty string</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">'node:url'</span>);
1328
1329<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>));
1330<span class="hljs-comment">// Prints xn--espaol-zwa.com</span>
1331<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>));
1332<span class="hljs-comment">// Prints xn--fiq228c.com</span>
1333<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>));
1334<span class="hljs-comment">// Prints an empty string</span></code><button class="copy-button">copy</button></pre>
1335<h4><code>url.domainToUnicode(domain)</code><span><a class="mark" href="#urldomaintounicodedomain" id="urldomaintounicodedomain">#</a></span><a aria-hidden="true" class="legacy" id="url_url_domaintounicode_domain"></a></h4>
1336<div class="api_metadata">
1337<details class="changelog"><summary>History</summary>
1338<table>
1339<tbody><tr><th>Version</th><th>Changes</th></tr>
1340<tr><td>v18.17.0</td>
1341<td><p>ICU requirement is removed.</p></td></tr>
1342<tr><td>v7.4.0, v6.13.0</td>
1343<td><p><span>Added in: v7.4.0, v6.13.0</span></p></td></tr>
1344</tbody></table>
1345</details>
1346</div>
1347<ul>
1348<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>
1349<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1350</ul>
1351<p>Returns the Unicode serialization of the <code>domain</code>. If <code>domain</code> is an invalid
1352domain, the empty string is returned.</p>
1353<p>It performs the inverse operation to <a href="#urldomaintoasciidomain"><code>url.domainToASCII()</code></a>.</p>
1354
1355<pre class="with-32-chars"><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">'node:url'</span>;
1356
1357<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>));
1358<span class="hljs-comment">// Prints español.com</span>
1359<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>));
1360<span class="hljs-comment">// Prints 中文.com</span>
1361<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>));
1362<span class="hljs-comment">// Prints an empty string</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">'node:url'</span>);
1363
1364<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>));
1365<span class="hljs-comment">// Prints español.com</span>
1366<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>));
1367<span class="hljs-comment">// Prints 中文.com</span>
1368<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>));
1369<span class="hljs-comment">// Prints an empty string</span></code><button class="copy-button">copy</button></pre>
1370<h4><code>url.fileURLToPath(url)</code><span><a class="mark" href="#urlfileurltopathurl" id="urlfileurltopathurl">#</a></span><a aria-hidden="true" class="legacy" id="url_url_fileurltopath_url"></a></h4>
1371<div class="api_metadata">
1372<span>Added in: v10.12.0</span>
1373</div>
1374<ul>
1375<li><code>url</code> <a href="url.html#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>
1376<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>
1377</ul>
1378<p>This function ensures the correct decodings of percent-encoded characters as
1379well as ensuring a cross-platform valid absolute path string.</p>
1380
1381<pre class="with-66-chars"><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">'node:url'</span>;
1382
1383<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>);
1384
1385<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>
1386<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///C:/path/'</span>);         <span class="hljs-comment">// Correct:   C:\path\ (Windows)</span>
1387
1388<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>
1389<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>
1390
1391<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>
1392<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///你好.txt'</span>);         <span class="hljs-comment">// Correct:   /你好.txt (POSIX)</span>
1393
1394<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>
1395<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">'node:url'</span>);
1396<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>
1397<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///C:/path/'</span>);         <span class="hljs-comment">// Correct:   C:\path\ (Windows)</span>
1398
1399<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>
1400<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>
1401
1402<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>
1403<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///你好.txt'</span>);         <span class="hljs-comment">// Correct:   /你好.txt (POSIX)</span>
1404
1405<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>
1406<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><button class="copy-button">copy</button></pre>
1407<h4><code>url.format(URL[, options])</code><span><a class="mark" href="#urlformaturl-options" id="urlformaturl-options">#</a></span><a aria-hidden="true" class="legacy" id="url_url_format_url_options"></a></h4>
1408<div class="api_metadata">
1409<span>Added in: v7.6.0</span>
1410</div>
1411<ul>
1412<li><code>URL</code> <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> A <a href="#the-whatwg-url-api">WHATWG URL</a> object</li>
1413<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1414<ul>
1415<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
1416username and password, <code>false</code> otherwise. <strong>Default:</strong> <code>true</code>.</li>
1417<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
1418fragment, <code>false</code> otherwise. <strong>Default:</strong> <code>true</code>.</li>
1419<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
1420search query, <code>false</code> otherwise. <strong>Default:</strong> <code>true</code>.</li>
1421<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
1422component of the URL string should be encoded directly as opposed to being
1423Punycode encoded. <strong>Default:</strong> <code>false</code>.</li>
1424</ul>
1425</li>
1426<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1427</ul>
1428<p>Returns a customizable serialization of a URL <code>String</code> representation of a
1429<a href="#the-whatwg-url-api">WHATWG URL</a> object.</p>
1430<p>The URL object has both a <code>toString()</code> method and <code>href</code> property that return
1431string serializations of the URL. These are not, however, customizable in
1432any way. The <code>url.format(URL[, options])</code> method allows for basic customization
1433of the output.</p>
1434
1435<pre class="with-48-chars"><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">'node:url'</span>;
1436<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>);
1437
1438<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
1439<span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
1440
1441<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-title function_">toString</span>());
1442<span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
1443
1444<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> }));
1445<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">'node:url'</span>);
1446<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>);
1447
1448<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
1449<span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
1450
1451<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-title function_">toString</span>());
1452<span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
1453
1454<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> }));
1455<span class="hljs-comment">// Prints 'https://測試/?abc'</span></code><button class="copy-button">copy</button></pre>
1456<h4><code>url.pathToFileURL(path)</code><span><a class="mark" href="#urlpathtofileurlpath" id="urlpathtofileurlpath">#</a></span><a aria-hidden="true" class="legacy" id="url_url_pathtofileurl_path"></a></h4>
1457<div class="api_metadata">
1458<span>Added in: v10.12.0</span>
1459</div>
1460<ul>
1461<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>
1462<li>Returns: <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> The file URL object.</li>
1463</ul>
1464<p>This function ensures that <code>path</code> is resolved absolutely, and that the URL
1465control characters are correctly encoded when converting into a File URL.</p>
1466
1467<pre class="with-66-chars"><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">'node:url'</span>;
1468
1469<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>
1470<span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/foo#1'</span>);              <span class="hljs-comment">// Correct:   file:///foo%231 (POSIX)</span>
1471
1472<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>
1473<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">'node:url'</span>);
1474<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(__filename);                  <span class="hljs-comment">// Incorrect: throws (POSIX)</span>
1475<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(__filename);                  <span class="hljs-comment">// Incorrect: C:\... (Windows)</span>
1476<span class="hljs-title function_">pathToFileURL</span>(__filename);            <span class="hljs-comment">// Correct:   file:///... (POSIX)</span>
1477<span class="hljs-title function_">pathToFileURL</span>(__filename);            <span class="hljs-comment">// Correct:   file:///C:/... (Windows)</span>
1478
1479<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>
1480<span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/foo#1'</span>);              <span class="hljs-comment">// Correct:   file:///foo%231 (POSIX)</span>
1481
1482<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>
1483<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><button class="copy-button">copy</button></pre>
1484<h4><code>url.urlToHttpOptions(url)</code><span><a class="mark" href="#urlurltohttpoptionsurl" id="urlurltohttpoptionsurl">#</a></span><a aria-hidden="true" class="legacy" id="url_url_urltohttpoptions_url"></a></h4>
1485<div class="api_metadata">
1486<details class="changelog"><summary>History</summary>
1487<table>
1488<tbody><tr><th>Version</th><th>Changes</th></tr>
1489<tr><td>v18.17.0</td>
1490<td><p>The returned object will also contain all the own enumerable properties of the <code>url</code> argument.</p></td></tr>
1491<tr><td>v15.7.0, v14.18.0</td>
1492<td><p><span>Added in: v15.7.0, v14.18.0</span></p></td></tr>
1493</tbody></table>
1494</details>
1495</div>
1496<ul>
1497<li><code>url</code> <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> The <a href="#the-whatwg-url-api">WHATWG URL</a> object to convert to an options object.</li>
1498<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Options object
1499<ul>
1500<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>
1501<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
1502request to.</li>
1503<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>
1504<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>
1505<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>
1506<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.
1507E.G. <code>'/index.html?page=12'</code>. An exception is thrown when the request path
1508contains illegal characters. Currently, only spaces are rejected but that
1509may change in the future.</li>
1510<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>
1511<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>
1512<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
1513Authorization header.</li>
1514</ul>
1515</li>
1516</ul>
1517<p>This utility function converts a URL object into an ordinary options object as
1518expected by the <a href="http.html#httprequestoptions-callback"><code>http.request()</code></a> and <a href="https.html#httpsrequestoptions-callback"><code>https.request()</code></a> APIs.</p>
1519
1520<pre class="with-49-chars"><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> { urlToHttpOptions } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>;
1521<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>);
1522
1523<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">urlToHttpOptions</span>(myURL));
1524<span class="hljs-comment">/*
1525{
1526  protocol: 'https:',
1527  hostname: 'xn--g6w251d',
1528  hash: '#foo',
1529  search: '?abc',
1530  pathname: '/',
1531  path: '/?abc',
1532  href: 'https://a:b@xn--g6w251d/?abc#foo',
1533  auth: 'a:b'
1534}
1535*/</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { urlToHttpOptions } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>);
1536<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>);
1537
1538<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">urlToHttpOptions</span>(myURL));
1539<span class="hljs-comment">/*
1540{
1541  protocol: 'https:',
1542  hostname: 'xn--g6w251d',
1543  hash: '#foo',
1544  search: '?abc',
1545  pathname: '/',
1546  path: '/?abc',
1547  href: 'https://a:b@xn--g6w251d/?abc#foo',
1548  auth: 'a:b'
1549}
1550*/</span></code><button class="copy-button">copy</button></pre>
1551</section><section><h3>Legacy URL API<span><a class="mark" href="#legacy-url-api" id="legacy-url-api">#</a></span><a aria-hidden="true" class="legacy" id="url_legacy_url_api"></a></h3>
1552<div class="api_metadata">
1553<details class="changelog"><summary>History</summary>
1554<table>
1555<tbody><tr><th>Version</th><th>Changes</th></tr>
1556<tr><td>v15.13.0, v14.17.0</td>
1557<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr>
1558<tr><td>v11.0.0</td>
1559<td><p>This API is deprecated.</p></td></tr>
1560</tbody></table>
1561</details>
1562</div>
1563<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use the WHATWG URL API instead.</div><p></p>
1564<h4>Legacy <code>urlObject</code><span><a class="mark" href="#legacy-urlobject" id="legacy-urlobject">#</a></span><a aria-hidden="true" class="legacy" id="url_legacy_urlobject"></a></h4>
1565<div class="api_metadata">
1566<details class="changelog"><summary>History</summary>
1567<table>
1568<tbody><tr><th>Version</th><th>Changes</th></tr>
1569<tr><td>v15.13.0, v14.17.0</td>
1570<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr>
1571<tr><td>v11.0.0</td>
1572<td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p></td></tr>
1573</tbody></table>
1574</details>
1575</div>
1576<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use the WHATWG URL API instead.</div><p></p>
1577<p>The legacy <code>urlObject</code> (<code>require('node:url').Url</code> or
1578<code>import { Url } from 'node:url'</code>) is
1579created and returned by the <code>url.parse()</code> function.</p>
1580<h5><code>urlObject.auth</code><span><a class="mark" href="#urlobjectauth" id="urlobjectauth">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_auth"></a></h5>
1581<p>The <code>auth</code> property is the username and password portion of the URL, also
1582referred to as <em>userinfo</em>. This string subset follows the <code>protocol</code> and
1583double slashes (if present) and precedes the <code>host</code> component, delimited by <code>@</code>.
1584The string is either the username, or it is the username and password separated
1585by <code>:</code>.</p>
1586<p>For example: <code>'user:pass'</code>.</p>
1587<h5><code>urlObject.hash</code><span><a class="mark" href="#urlobjecthash" id="urlobjecthash">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_hash"></a></h5>
1588<p>The <code>hash</code> property is the fragment identifier portion of the URL including the
1589leading <code>#</code> character.</p>
1590<p>For example: <code>'#hash'</code>.</p>
1591<h5><code>urlObject.host</code><span><a class="mark" href="#urlobjecthost" id="urlobjecthost">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_host"></a></h5>
1592<p>The <code>host</code> property is the full lower-cased host portion of the URL, including
1593the <code>port</code> if specified.</p>
1594<p>For example: <code>'sub.example.com:8080'</code>.</p>
1595<h5><code>urlObject.hostname</code><span><a class="mark" href="#urlobjecthostname" id="urlobjecthostname">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_hostname"></a></h5>
1596<p>The <code>hostname</code> property is the lower-cased host name portion of the <code>host</code>
1597component <em>without</em> the <code>port</code> included.</p>
1598<p>For example: <code>'sub.example.com'</code>.</p>
1599<h5><code>urlObject.href</code><span><a class="mark" href="#urlobjecthref" id="urlobjecthref">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_href"></a></h5>
1600<p>The <code>href</code> property is the full URL string that was parsed with both the
1601<code>protocol</code> and <code>host</code> components converted to lower-case.</p>
1602<p>For example: <code>'http://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'</code>.</p>
1603<h5><code>urlObject.path</code><span><a class="mark" href="#urlobjectpath" id="urlobjectpath">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_path"></a></h5>
1604<p>The <code>path</code> property is a concatenation of the <code>pathname</code> and <code>search</code>
1605components.</p>
1606<p>For example: <code>'/p/a/t/h?query=string'</code>.</p>
1607<p>No decoding of the <code>path</code> is performed.</p>
1608<h5><code>urlObject.pathname</code><span><a class="mark" href="#urlobjectpathname" id="urlobjectpathname">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_pathname"></a></h5>
1609<p>The <code>pathname</code> property consists of the entire path section of the URL. This
1610is everything following the <code>host</code> (including the <code>port</code>) and before the start
1611of the <code>query</code> or <code>hash</code> components, delimited by either the ASCII question
1612mark (<code>?</code>) or hash (<code>#</code>) characters.</p>
1613<p>For example: <code>'/p/a/t/h'</code>.</p>
1614<p>No decoding of the path string is performed.</p>
1615<h5><code>urlObject.port</code><span><a class="mark" href="#urlobjectport" id="urlobjectport">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_port"></a></h5>
1616<p>The <code>port</code> property is the numeric port portion of the <code>host</code> component.</p>
1617<p>For example: <code>'8080'</code>.</p>
1618<h5><code>urlObject.protocol</code><span><a class="mark" href="#urlobjectprotocol" id="urlobjectprotocol">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_protocol"></a></h5>
1619<p>The <code>protocol</code> property identifies the URL's lower-cased protocol scheme.</p>
1620<p>For example: <code>'http:'</code>.</p>
1621<h5><code>urlObject.query</code><span><a class="mark" href="#urlobjectquery" id="urlobjectquery">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_query"></a></h5>
1622<p>The <code>query</code> property is either the query string without the leading ASCII
1623question mark (<code>?</code>), or an object returned by the <a href="querystring.html"><code>querystring</code></a> module's
1624<code>parse()</code> method. Whether the <code>query</code> property is a string or object is
1625determined by the <code>parseQueryString</code> argument passed to <code>url.parse()</code>.</p>
1626<p>For example: <code>'query=string'</code> or <code>{'query': 'string'}</code>.</p>
1627<p>If returned as a string, no decoding of the query string is performed. If
1628returned as an object, both keys and values are decoded.</p>
1629<h5><code>urlObject.search</code><span><a class="mark" href="#urlobjectsearch" id="urlobjectsearch">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_search"></a></h5>
1630<p>The <code>search</code> property consists of the entire "query string" portion of the
1631URL, including the leading ASCII question mark (<code>?</code>) character.</p>
1632<p>For example: <code>'?query=string'</code>.</p>
1633<p>No decoding of the query string is performed.</p>
1634<h5><code>urlObject.slashes</code><span><a class="mark" href="#urlobjectslashes" id="urlobjectslashes">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_slashes"></a></h5>
1635<p>The <code>slashes</code> property is a <code>boolean</code> with a value of <code>true</code> if two ASCII
1636forward-slash characters (<code>/</code>) are required following the colon in the
1637<code>protocol</code>.</p>
1638<h4><code>url.format(urlObject)</code><span><a class="mark" href="#urlformaturlobject" id="urlformaturlobject">#</a></span><a aria-hidden="true" class="legacy" id="url_url_format_urlobject"></a></h4>
1639<div class="api_metadata">
1640<details class="changelog"><summary>History</summary>
1641<table>
1642<tbody><tr><th>Version</th><th>Changes</th></tr>
1643<tr><td>v17.0.0</td>
1644<td><p>Now throws an <code>ERR_INVALID_URL</code> exception when Punycode conversion of a hostname introduces changes that could cause the URL to be re-parsed differently.</p></td></tr>
1645<tr><td>v15.13.0, v14.17.0</td>
1646<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr>
1647<tr><td>v11.0.0</td>
1648<td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p></td></tr>
1649<tr><td>v7.0.0</td>
1650<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>
1651<tr><td>v0.1.25</td>
1652<td><p><span>Added in: v0.1.25</span></p></td></tr>
1653</tbody></table>
1654</details>
1655</div>
1656<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use the WHATWG URL API instead.</div><p></p>
1657<ul>
1658<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
1659constructed otherwise). If a string, it is converted to an object by passing
1660it to <code>url.parse()</code>.</li>
1661</ul>
1662<p>The <code>url.format()</code> method returns a formatted URL string derived from
1663<code>urlObject</code>.</p>
1664<pre><code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>);
1665url.<span class="hljs-title function_">format</span>({
1666  <span class="hljs-attr">protocol</span>: <span class="hljs-string">'https'</span>,
1667  <span class="hljs-attr">hostname</span>: <span class="hljs-string">'example.com'</span>,
1668  <span class="hljs-attr">pathname</span>: <span class="hljs-string">'/some/path'</span>,
1669  <span class="hljs-attr">query</span>: {
1670    <span class="hljs-attr">page</span>: <span class="hljs-number">1</span>,
1671    <span class="hljs-attr">format</span>: <span class="hljs-string">'json'</span>,
1672  },
1673});
1674
1675<span class="hljs-comment">// => 'https://example.com/some/path?page=1&#x26;format=json'</span></code> <button class="copy-button">copy</button></pre>
1676<p>If <code>urlObject</code> is not an object or a string, <code>url.format()</code> will throw a
1677<a href="errors.html#class-typeerror"><code>TypeError</code></a>.</p>
1678<p>The formatting process operates as follows:</p>
1679<ul>
1680<li>A new empty string <code>result</code> is created.</li>
1681<li>If <code>urlObject.protocol</code> is a string, it is appended as-is to <code>result</code>.</li>
1682<li>Otherwise, if <code>urlObject.protocol</code> is not <code>undefined</code> and is not a string, an
1683<a href="errors.html#class-error"><code>Error</code></a> is thrown.</li>
1684<li>For all string values of <code>urlObject.protocol</code> that <em>do not end</em> with an ASCII
1685colon (<code>:</code>) character, the literal string <code>:</code> will be appended to <code>result</code>.</li>
1686<li>If either of the following conditions is true, then the literal string <code>//</code>
1687will be appended to <code>result</code>:
1688<ul>
1689<li><code>urlObject.slashes</code> property is true;</li>
1690<li><code>urlObject.protocol</code> begins with <code>http</code>, <code>https</code>, <code>ftp</code>, <code>gopher</code>, or
1691<code>file</code>;</li>
1692</ul>
1693</li>
1694<li>If the value of the <code>urlObject.auth</code> property is truthy, and either
1695<code>urlObject.host</code> or <code>urlObject.hostname</code> are not <code>undefined</code>, the value of
1696<code>urlObject.auth</code> will be coerced into a string and appended to <code>result</code>
1697followed by the literal string <code>@</code>.</li>
1698<li>If the <code>urlObject.host</code> property is <code>undefined</code> then:
1699<ul>
1700<li>If the <code>urlObject.hostname</code> is a string, it is appended to <code>result</code>.</li>
1701<li>Otherwise, if <code>urlObject.hostname</code> is not <code>undefined</code> and is not a string,
1702an <a href="errors.html#class-error"><code>Error</code></a> is thrown.</li>
1703<li>If the <code>urlObject.port</code> property value is truthy, and <code>urlObject.hostname</code>
1704is not <code>undefined</code>:
1705<ul>
1706<li>The literal string <code>:</code> is appended to <code>result</code>, and</li>
1707<li>The value of <code>urlObject.port</code> is coerced to a string and appended to
1708<code>result</code>.</li>
1709</ul>
1710</li>
1711</ul>
1712</li>
1713<li>Otherwise, if the <code>urlObject.host</code> property value is truthy, the value of
1714<code>urlObject.host</code> is coerced to a string and appended to <code>result</code>.</li>
1715<li>If the <code>urlObject.pathname</code> property is a string that is not an empty string:
1716<ul>
1717<li>If the <code>urlObject.pathname</code> <em>does not start</em> with an ASCII forward slash
1718(<code>/</code>), then the literal string <code>'/'</code> is appended to <code>result</code>.</li>
1719<li>The value of <code>urlObject.pathname</code> is appended to <code>result</code>.</li>
1720</ul>
1721</li>
1722<li>Otherwise, if <code>urlObject.pathname</code> is not <code>undefined</code> and is not a string, an
1723<a href="errors.html#class-error"><code>Error</code></a> is thrown.</li>
1724<li>If the <code>urlObject.search</code> property is <code>undefined</code> and if the <code>urlObject.query</code>
1725property is an <code>Object</code>, the literal string <code>?</code> is appended to <code>result</code>
1726followed by the output of calling the <a href="querystring.html"><code>querystring</code></a> module's <code>stringify()</code>
1727method passing the value of <code>urlObject.query</code>.</li>
1728<li>Otherwise, if <code>urlObject.search</code> is a string:
1729<ul>
1730<li>If the value of <code>urlObject.search</code> <em>does not start</em> with the ASCII question
1731mark (<code>?</code>) character, the literal string <code>?</code> is appended to <code>result</code>.</li>
1732<li>The value of <code>urlObject.search</code> is appended to <code>result</code>.</li>
1733</ul>
1734</li>
1735<li>Otherwise, if <code>urlObject.search</code> is not <code>undefined</code> and is not a string, an
1736<a href="errors.html#class-error"><code>Error</code></a> is thrown.</li>
1737<li>If the <code>urlObject.hash</code> property is a string:
1738<ul>
1739<li>If the value of <code>urlObject.hash</code> <em>does not start</em> with the ASCII hash (<code>#</code>)
1740character, the literal string <code>#</code> is appended to <code>result</code>.</li>
1741<li>The value of <code>urlObject.hash</code> is appended to <code>result</code>.</li>
1742</ul>
1743</li>
1744<li>Otherwise, if the <code>urlObject.hash</code> property is not <code>undefined</code> and is not a
1745string, an <a href="errors.html#class-error"><code>Error</code></a> is thrown.</li>
1746<li><code>result</code> is returned.</li>
1747</ul>
1748<h4><code>url.parse(urlString[, parseQueryString[, slashesDenoteHost]])</code><span><a class="mark" href="#urlparseurlstring-parsequerystring-slashesdenotehost" id="urlparseurlstring-parsequerystring-slashesdenotehost">#</a></span><a aria-hidden="true" class="legacy" id="url_url_parse_urlstring_parsequerystring_slashesdenotehost"></a></h4>
1749<div class="api_metadata">
1750<details class="changelog"><summary>History</summary>
1751<table>
1752<tbody><tr><th>Version</th><th>Changes</th></tr>
1753<tr><td>v18.13.0</td>
1754<td><p>Documentation-only deprecation.</p></td></tr>
1755<tr><td>v15.13.0, v14.17.0</td>
1756<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr>
1757<tr><td>v11.14.0</td>
1758<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>
1759<tr><td>v11.0.0</td>
1760<td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p></td></tr>
1761<tr><td>v9.0.0</td>
1762<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>
1763<tr><td>v0.1.25</td>
1764<td><p><span>Added in: v0.1.25</span></p></td></tr>
1765</tbody></table>
1766</details>
1767</div>
1768<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use the WHATWG URL API instead.</div><p></p>
1769<ul>
1770<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>
1771<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
1772be set to an object returned by the <a href="querystring.html"><code>querystring</code></a> module's <code>parse()</code>
1773method. If <code>false</code>, the <code>query</code> property on the returned URL object will be an
1774unparsed, undecoded string. <strong>Default:</strong> <code>false</code>.</li>
1775<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
1776string <code>//</code> and preceding the next <code>/</code> will be interpreted as the <code>host</code>.
1777For instance, given <code>//foo/bar</code>, the result would be
1778<code>{host: 'foo', pathname: '/bar'}</code> rather than <code>{pathname: '//foo/bar'}</code>.
1779<strong>Default:</strong> <code>false</code>.</li>
1780</ul>
1781<p>The <code>url.parse()</code> method takes a URL string, parses it, and returns a URL
1782object.</p>
1783<p>A <code>TypeError</code> is thrown if <code>urlString</code> is not a string.</p>
1784<p>A <code>URIError</code> is thrown if the <code>auth</code> property is present but cannot be decoded.</p>
1785<p><code>url.parse()</code> uses a lenient, non-standard algorithm for parsing URL
1786strings. It is prone to security issues such as <a href="https://hackerone.com/reports/678487">host name spoofing</a>
1787and incorrect handling of usernames and passwords. Do not use with untrusted
1788input. CVEs are not issued for <code>url.parse()</code> vulnerabilities. Use the
1789<a href="#the-whatwg-url-api">WHATWG URL</a> API instead.</p>
1790<h4><code>url.resolve(from, to)</code><span><a class="mark" href="#urlresolvefrom-to" id="urlresolvefrom-to">#</a></span><a aria-hidden="true" class="legacy" id="url_url_resolve_from_to"></a></h4>
1791<div class="api_metadata">
1792<details class="changelog"><summary>History</summary>
1793<table>
1794<tbody><tr><th>Version</th><th>Changes</th></tr>
1795<tr><td>v15.13.0, v14.17.0</td>
1796<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr>
1797<tr><td>v11.0.0</td>
1798<td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p></td></tr>
1799<tr><td>v6.6.0</td>
1800<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>
1801<tr><td>v6.0.0</td>
1802<td><p>The <code>auth</code> fields is cleared now the <code>to</code> parameter contains a hostname.</p></td></tr>
1803<tr><td>v6.5.0, v4.6.2</td>
1804<td><p>The <code>port</code> field is copied correctly now.</p></td></tr>
1805<tr><td>v0.1.25</td>
1806<td><p><span>Added in: v0.1.25</span></p></td></tr>
1807</tbody></table>
1808</details>
1809</div>
1810<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use the WHATWG URL API instead.</div><p></p>
1811<ul>
1812<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 to use if <code>to</code> is a relative URL.</li>
1813<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 target URL to resolve.</li>
1814</ul>
1815<p>The <code>url.resolve()</code> method resolves a target URL relative to a base URL in a
1816manner similar to that of a web browser resolving an anchor tag.</p>
1817<pre><code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>);
1818url.<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>
1819url.<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>
1820url.<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> <button class="copy-button">copy</button></pre>
1821<p>To achieve the same result using the WHATWG URL API:</p>
1822<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>) {
1823  <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>));
1824  <span class="hljs-keyword">if</span> (resolvedUrl.<span class="hljs-property">protocol</span> === <span class="hljs-string">'resolve:'</span>) {
1825    <span class="hljs-comment">// `from` is a relative URL.</span>
1826    <span class="hljs-keyword">const</span> { pathname, search, hash } = resolvedUrl;
1827    <span class="hljs-keyword">return</span> pathname + search + hash;
1828  }
1829  <span class="hljs-keyword">return</span> resolvedUrl.<span class="hljs-title function_">toString</span>();
1830}
1831
1832<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>
1833<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>
1834<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> <button class="copy-button">copy</button></pre>
1835<p><a id="whatwg-percent-encoding"></a></p>
1836</section><section><h3>Percent-encoding in URLs<span><a class="mark" href="#percent-encoding-in-urls" id="percent-encoding-in-urls">#</a></span><a aria-hidden="true" class="legacy" id="url_percent_encoding_in_urls"></a></h3>
1837<p>URLs are permitted to only contain a certain range of characters. Any character
1838falling outside of that range must be encoded. How such characters are encoded,
1839and which characters to encode depends entirely on where the character is
1840located within the structure of the URL.</p>
1841<h4>Legacy API<span><a class="mark" href="#legacy-api" id="legacy-api">#</a></span><a aria-hidden="true" class="legacy" id="url_legacy_api"></a></h4>
1842<p>Within the Legacy API, spaces (<code>' '</code>) and the following characters will be
1843automatically escaped in the properties of URL objects:</p>
1844<pre><code class="language-text">&#x3C; > " ` \r \n \t { } | \ ^ '</code> <button class="copy-button">copy</button></pre>
1845<p>For example, the ASCII space character (<code>' '</code>) is encoded as <code>%20</code>. The ASCII
1846forward slash (<code>/</code>) character is encoded as <code>%3C</code>.</p>
1847<h4>WHATWG API<span><a class="mark" href="#whatwg-api" id="whatwg-api">#</a></span><a aria-hidden="true" class="legacy" id="url_whatwg_api"></a></h4>
1848<p>The <a href="https://url.spec.whatwg.org/">WHATWG URL Standard</a> uses a more selective and fine grained approach to
1849selecting encoded characters than that used by the Legacy API.</p>
1850<p>The WHATWG algorithm defines four "percent-encode sets" that describe ranges
1851of characters that must be percent-encoded:</p>
1852<ul>
1853<li>
1854<p>The <em>C0 control percent-encode set</em> includes code points in range U+0000 to
1855U+001F (inclusive) and all code points greater than U+007E.</p>
1856</li>
1857<li>
1858<p>The <em>fragment percent-encode set</em> includes the <em>C0 control percent-encode set</em>
1859and code points U+0020, U+0022, U+003C, U+003E, and U+0060.</p>
1860</li>
1861<li>
1862<p>The <em>path percent-encode set</em> includes the <em>C0 control percent-encode set</em>
1863and code points U+0020, U+0022, U+0023, U+003C, U+003E, U+003F, U+0060,
1864U+007B, and U+007D.</p>
1865</li>
1866<li>
1867<p>The <em>userinfo encode set</em> includes the <em>path percent-encode set</em> and code
1868points U+002F, U+003A, U+003B, U+003D, U+0040, U+005B, U+005C, U+005D,
1869U+005E, and U+007C.</p>
1870</li>
1871</ul>
1872<p>The <em>userinfo percent-encode set</em> is used exclusively for username and
1873passwords encoded within the URL. The <em>path percent-encode set</em> is used for the
1874path of most URLs. The <em>fragment percent-encode set</em> is used for URL fragments.
1875The <em>C0 control percent-encode set</em> is used for host and path under certain
1876specific conditions, in addition to all other cases.</p>
1877<p>When non-ASCII characters appear within a host name, the host name is encoded
1878using 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
1879<em>both</em> Punycode encoded and percent-encoded characters:</p>
1880<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>);
1881<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
1882<span class="hljs-comment">// Prints https://xn--1xa.example.com/foo</span>
1883<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">origin</span>);
1884<span class="hljs-comment">// Prints https://xn--1xa.example.com</span></code> <button class="copy-button">copy</button></pre></section>
1885        <!-- API END -->
1886      </div>
1887    </div>
1888  </div>
1889</body>
1890</html>
1891