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