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