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