11cb0ef41Sopenharmony_ci<!DOCTYPE html> 21cb0ef41Sopenharmony_ci<html lang="en"> 31cb0ef41Sopenharmony_ci<head> 41cb0ef41Sopenharmony_ci <meta charset="utf-8"> 51cb0ef41Sopenharmony_ci <meta name="viewport" content="width=device-width"> 61cb0ef41Sopenharmony_ci <meta name="nodejs.org:node-version" content="v18.20.1"> 71cb0ef41Sopenharmony_ci <title>URL | Node.js v18.20.1 Documentation</title> 81cb0ef41Sopenharmony_ci <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 91cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/style.css"> 101cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/hljs.css"> 111cb0ef41Sopenharmony_ci <link rel="canonical" href="https://nodejs.org/api/url.html"> 121cb0ef41Sopenharmony_ci <script async defer src="assets/api.js" type="text/javascript"></script> 131cb0ef41Sopenharmony_ci <style>@media(max-width:478px){.with-32-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:726px){.with-63-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:750px){.with-66-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:606px){.with-48-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:614px){.with-49-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style> 141cb0ef41Sopenharmony_ci</head> 151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-url"> 161cb0ef41Sopenharmony_ci <div id="content" class="clearfix"> 171cb0ef41Sopenharmony_ci <div id="column2" class="interior"> 181cb0ef41Sopenharmony_ci <div id="intro" class="interior"> 191cb0ef41Sopenharmony_ci <a href="/" title="Go back to the home page"> 201cb0ef41Sopenharmony_ci Node.js 211cb0ef41Sopenharmony_ci </a> 221cb0ef41Sopenharmony_ci </div> 231cb0ef41Sopenharmony_ci <ul> 241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 261cb0ef41Sopenharmony_ci</ul> 271cb0ef41Sopenharmony_ci<hr class="line"> 281cb0ef41Sopenharmony_ci<ul> 291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url active">URL</a></li> 811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 891cb0ef41Sopenharmony_ci</ul> 901cb0ef41Sopenharmony_ci<hr class="line"> 911cb0ef41Sopenharmony_ci<ul> 921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 931cb0ef41Sopenharmony_ci</ul> 941cb0ef41Sopenharmony_ci </div> 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci <div id="column1" data-id="url" class="interior"> 971cb0ef41Sopenharmony_ci <header class="header"> 981cb0ef41Sopenharmony_ci <div class="header-container"> 991cb0ef41Sopenharmony_ci <h1>Node.js v18.20.1 documentation</h1> 1001cb0ef41Sopenharmony_ci <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 1011cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 1021cb0ef41Sopenharmony_ci <path fill="none" d="M0 0h24v24H0z" /> 1031cb0ef41Sopenharmony_ci <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"/> 1041cb0ef41Sopenharmony_ci <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"/> 1051cb0ef41Sopenharmony_ci </svg> 1061cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 1071cb0ef41Sopenharmony_ci <path d="M0 0h24v24H0z" fill="none" /> 1081cb0ef41Sopenharmony_ci <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"/> 1091cb0ef41Sopenharmony_ci </svg> 1101cb0ef41Sopenharmony_ci </button> 1111cb0ef41Sopenharmony_ci </div> 1121cb0ef41Sopenharmony_ci <div id="gtoc"> 1131cb0ef41Sopenharmony_ci <ul> 1141cb0ef41Sopenharmony_ci <li class="pinned-header">Node.js v18.20.1</li> 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci <li class="picker-header"> 1171cb0ef41Sopenharmony_ci <a href="#"> 1181cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 1191cb0ef41Sopenharmony_ci Table of contents 1201cb0ef41Sopenharmony_ci </a> 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci <div class="picker"><div class="toc"><ul> 1231cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#url">URL</a></span> 1241cb0ef41Sopenharmony_ci<ul> 1251cb0ef41Sopenharmony_ci<li><a href="#url-strings-and-url-objects">URL strings and URL objects</a> 1261cb0ef41Sopenharmony_ci<ul> 1271cb0ef41Sopenharmony_ci<li><a href="#constructing-a-url-from-component-parts-and-getting-the-constructed-string">Constructing a URL from component parts and getting the constructed string</a></li> 1281cb0ef41Sopenharmony_ci</ul> 1291cb0ef41Sopenharmony_ci</li> 1301cb0ef41Sopenharmony_ci<li><a href="#the-whatwg-url-api">The WHATWG URL API</a> 1311cb0ef41Sopenharmony_ci<ul> 1321cb0ef41Sopenharmony_ci<li><a href="#class-url">Class: <code>URL</code></a> 1331cb0ef41Sopenharmony_ci<ul> 1341cb0ef41Sopenharmony_ci<li><a href="#new-urlinput-base"><code>new URL(input[, base])</code></a></li> 1351cb0ef41Sopenharmony_ci<li><a href="#urlhash"><code>url.hash</code></a></li> 1361cb0ef41Sopenharmony_ci<li><a href="#urlhost"><code>url.host</code></a></li> 1371cb0ef41Sopenharmony_ci<li><a href="#urlhostname"><code>url.hostname</code></a></li> 1381cb0ef41Sopenharmony_ci<li><a href="#urlhref"><code>url.href</code></a></li> 1391cb0ef41Sopenharmony_ci<li><a href="#urlorigin"><code>url.origin</code></a></li> 1401cb0ef41Sopenharmony_ci<li><a href="#urlpassword"><code>url.password</code></a></li> 1411cb0ef41Sopenharmony_ci<li><a href="#urlpathname"><code>url.pathname</code></a></li> 1421cb0ef41Sopenharmony_ci<li><a href="#urlport"><code>url.port</code></a></li> 1431cb0ef41Sopenharmony_ci<li><a href="#urlprotocol"><code>url.protocol</code></a> 1441cb0ef41Sopenharmony_ci<ul> 1451cb0ef41Sopenharmony_ci<li><a href="#special-schemes">Special schemes</a></li> 1461cb0ef41Sopenharmony_ci</ul> 1471cb0ef41Sopenharmony_ci</li> 1481cb0ef41Sopenharmony_ci<li><a href="#urlsearch"><code>url.search</code></a></li> 1491cb0ef41Sopenharmony_ci<li><a href="#urlsearchparams"><code>url.searchParams</code></a></li> 1501cb0ef41Sopenharmony_ci<li><a href="#urlusername"><code>url.username</code></a></li> 1511cb0ef41Sopenharmony_ci<li><a href="#urltostring"><code>url.toString()</code></a></li> 1521cb0ef41Sopenharmony_ci<li><a href="#urltojson"><code>url.toJSON()</code></a></li> 1531cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#urlcreateobjecturlblob"><code>URL.createObjectURL(blob)</code></a></span></li> 1541cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#urlrevokeobjecturlid"><code>URL.revokeObjectURL(id)</code></a></span></li> 1551cb0ef41Sopenharmony_ci<li><a href="#urlcanparseinput-base"><code>URL.canParse(input[, base])</code></a></li> 1561cb0ef41Sopenharmony_ci</ul> 1571cb0ef41Sopenharmony_ci</li> 1581cb0ef41Sopenharmony_ci<li><a href="#class-urlsearchparams">Class: <code>URLSearchParams</code></a> 1591cb0ef41Sopenharmony_ci<ul> 1601cb0ef41Sopenharmony_ci<li><a href="#new-urlsearchparams"><code>new URLSearchParams()</code></a></li> 1611cb0ef41Sopenharmony_ci<li><a href="#new-urlsearchparamsstring"><code>new URLSearchParams(string)</code></a></li> 1621cb0ef41Sopenharmony_ci<li><a href="#new-urlsearchparamsobj"><code>new URLSearchParams(obj)</code></a></li> 1631cb0ef41Sopenharmony_ci<li><a href="#new-urlsearchparamsiterable"><code>new URLSearchParams(iterable)</code></a></li> 1641cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamsappendname-value"><code>urlSearchParams.append(name, value)</code></a></li> 1651cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamsdeletename-value"><code>urlSearchParams.delete(name[, value])</code></a></li> 1661cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamsentries"><code>urlSearchParams.entries()</code></a></li> 1671cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamsforeachfn-thisarg"><code>urlSearchParams.forEach(fn[, thisArg])</code></a></li> 1681cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamsgetname"><code>urlSearchParams.get(name)</code></a></li> 1691cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamsgetallname"><code>urlSearchParams.getAll(name)</code></a></li> 1701cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamshasname-value"><code>urlSearchParams.has(name[, value])</code></a></li> 1711cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamskeys"><code>urlSearchParams.keys()</code></a></li> 1721cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamssetname-value"><code>urlSearchParams.set(name, value)</code></a></li> 1731cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamssize"><code>urlSearchParams.size</code></a></li> 1741cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamssort"><code>urlSearchParams.sort()</code></a></li> 1751cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamstostring"><code>urlSearchParams.toString()</code></a></li> 1761cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamsvalues"><code>urlSearchParams.values()</code></a></li> 1771cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamssymboliterator"><code>urlSearchParams[Symbol.iterator]()</code></a></li> 1781cb0ef41Sopenharmony_ci</ul> 1791cb0ef41Sopenharmony_ci</li> 1801cb0ef41Sopenharmony_ci<li><a href="#urldomaintoasciidomain"><code>url.domainToASCII(domain)</code></a></li> 1811cb0ef41Sopenharmony_ci<li><a href="#urldomaintounicodedomain"><code>url.domainToUnicode(domain)</code></a></li> 1821cb0ef41Sopenharmony_ci<li><a href="#urlfileurltopathurl"><code>url.fileURLToPath(url)</code></a></li> 1831cb0ef41Sopenharmony_ci<li><a href="#urlformaturl-options"><code>url.format(URL[, options])</code></a></li> 1841cb0ef41Sopenharmony_ci<li><a href="#urlpathtofileurlpath"><code>url.pathToFileURL(path)</code></a></li> 1851cb0ef41Sopenharmony_ci<li><a href="#urlurltohttpoptionsurl"><code>url.urlToHttpOptions(url)</code></a></li> 1861cb0ef41Sopenharmony_ci</ul> 1871cb0ef41Sopenharmony_ci</li> 1881cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#legacy-url-api">Legacy URL API</a></span> 1891cb0ef41Sopenharmony_ci<ul> 1901cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#legacy-urlobject">Legacy <code>urlObject</code></a></span> 1911cb0ef41Sopenharmony_ci<ul> 1921cb0ef41Sopenharmony_ci<li><a href="#urlobjectauth"><code>urlObject.auth</code></a></li> 1931cb0ef41Sopenharmony_ci<li><a href="#urlobjecthash"><code>urlObject.hash</code></a></li> 1941cb0ef41Sopenharmony_ci<li><a href="#urlobjecthost"><code>urlObject.host</code></a></li> 1951cb0ef41Sopenharmony_ci<li><a href="#urlobjecthostname"><code>urlObject.hostname</code></a></li> 1961cb0ef41Sopenharmony_ci<li><a href="#urlobjecthref"><code>urlObject.href</code></a></li> 1971cb0ef41Sopenharmony_ci<li><a href="#urlobjectpath"><code>urlObject.path</code></a></li> 1981cb0ef41Sopenharmony_ci<li><a href="#urlobjectpathname"><code>urlObject.pathname</code></a></li> 1991cb0ef41Sopenharmony_ci<li><a href="#urlobjectport"><code>urlObject.port</code></a></li> 2001cb0ef41Sopenharmony_ci<li><a href="#urlobjectprotocol"><code>urlObject.protocol</code></a></li> 2011cb0ef41Sopenharmony_ci<li><a href="#urlobjectquery"><code>urlObject.query</code></a></li> 2021cb0ef41Sopenharmony_ci<li><a href="#urlobjectsearch"><code>urlObject.search</code></a></li> 2031cb0ef41Sopenharmony_ci<li><a href="#urlobjectslashes"><code>urlObject.slashes</code></a></li> 2041cb0ef41Sopenharmony_ci</ul> 2051cb0ef41Sopenharmony_ci</li> 2061cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#urlformaturlobject"><code>url.format(urlObject)</code></a></span></li> 2071cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse(urlString[, parseQueryString[, slashesDenoteHost]])</code></a></span></li> 2081cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#urlresolvefrom-to"><code>url.resolve(from, to)</code></a></span></li> 2091cb0ef41Sopenharmony_ci</ul> 2101cb0ef41Sopenharmony_ci</li> 2111cb0ef41Sopenharmony_ci<li><a href="#percent-encoding-in-urls">Percent-encoding in URLs</a> 2121cb0ef41Sopenharmony_ci<ul> 2131cb0ef41Sopenharmony_ci<li><a href="#legacy-api">Legacy API</a></li> 2141cb0ef41Sopenharmony_ci<li><a href="#whatwg-api">WHATWG API</a></li> 2151cb0ef41Sopenharmony_ci</ul> 2161cb0ef41Sopenharmony_ci</li> 2171cb0ef41Sopenharmony_ci</ul> 2181cb0ef41Sopenharmony_ci</li> 2191cb0ef41Sopenharmony_ci</ul></div></div> 2201cb0ef41Sopenharmony_ci </li> 2211cb0ef41Sopenharmony_ci 2221cb0ef41Sopenharmony_ci 2231cb0ef41Sopenharmony_ci <li class="picker-header"> 2241cb0ef41Sopenharmony_ci <a href="#"> 2251cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 2261cb0ef41Sopenharmony_ci Index 2271cb0ef41Sopenharmony_ci </a> 2281cb0ef41Sopenharmony_ci 2291cb0ef41Sopenharmony_ci <div class="picker"><ul> 2301cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 2311cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 2321cb0ef41Sopenharmony_ci 2331cb0ef41Sopenharmony_ci <li> 2341cb0ef41Sopenharmony_ci <a href="index.html">Index</a> 2351cb0ef41Sopenharmony_ci </li> 2361cb0ef41Sopenharmony_ci </ul> 2371cb0ef41Sopenharmony_ci 2381cb0ef41Sopenharmony_ci<hr class="line"> 2391cb0ef41Sopenharmony_ci<ul> 2401cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 2411cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 2421cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 2431cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 2441cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 2451cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 2461cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 2471cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 2481cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 2491cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 2501cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 2511cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 2521cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 2531cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 2541cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 2551cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 2561cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 2571cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 2581cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 2591cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 2601cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 2611cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 2621cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 2631cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 2641cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 2651cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 2661cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 2671cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 2681cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 2691cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 2701cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 2711cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 2721cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 2731cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 2741cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 2751cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 2761cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 2771cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 2781cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 2791cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 2801cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 2811cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 2821cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 2831cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 2841cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 2851cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 2861cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 2871cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 2881cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 2891cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 2901cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 2911cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url active">URL</a></li> 2921cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 2931cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 2941cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 2951cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 2961cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 2971cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 2981cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 2991cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 3001cb0ef41Sopenharmony_ci</ul> 3011cb0ef41Sopenharmony_ci<hr class="line"> 3021cb0ef41Sopenharmony_ci<ul> 3031cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 3041cb0ef41Sopenharmony_ci</ul></div> 3051cb0ef41Sopenharmony_ci </li> 3061cb0ef41Sopenharmony_ci 3071cb0ef41Sopenharmony_ci 3081cb0ef41Sopenharmony_ci <li class="picker-header"> 3091cb0ef41Sopenharmony_ci <a href="#"> 3101cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 3111cb0ef41Sopenharmony_ci Other versions 3121cb0ef41Sopenharmony_ci </a> 3131cb0ef41Sopenharmony_ci <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/url.html">21.x</a></li> 3141cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/url.html">20.x <b>LTS</b></a></li> 3151cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/url.html">19.x</a></li> 3161cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/url.html">18.x <b>LTS</b></a></li> 3171cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/url.html">17.x</a></li> 3181cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/url.html">16.x</a></li> 3191cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/url.html">15.x</a></li> 3201cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/url.html">14.x</a></li> 3211cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/url.html">13.x</a></li> 3221cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/url.html">12.x</a></li> 3231cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/url.html">11.x</a></li> 3241cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/url.html">10.x</a></li> 3251cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v9.x/api/url.html">9.x</a></li> 3261cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v8.x/api/url.html">8.x</a></li> 3271cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v7.x/api/url.html">7.x</a></li> 3281cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v6.x/api/url.html">6.x</a></li> 3291cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v5.x/api/url.html">5.x</a></li> 3301cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v4.x/api/url.html">4.x</a></li> 3311cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/url.html">0.12.x</a></li> 3321cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/url.html">0.10.x</a></li></ol></div> 3331cb0ef41Sopenharmony_ci </li> 3341cb0ef41Sopenharmony_ci 3351cb0ef41Sopenharmony_ci <li class="picker-header"> 3361cb0ef41Sopenharmony_ci <a href="#"> 3371cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 3381cb0ef41Sopenharmony_ci Options 3391cb0ef41Sopenharmony_ci </a> 3401cb0ef41Sopenharmony_ci 3411cb0ef41Sopenharmony_ci <div class="picker"> 3421cb0ef41Sopenharmony_ci <ul> 3431cb0ef41Sopenharmony_ci <li> 3441cb0ef41Sopenharmony_ci <a href="all.html">View on single page</a> 3451cb0ef41Sopenharmony_ci </li> 3461cb0ef41Sopenharmony_ci <li> 3471cb0ef41Sopenharmony_ci <a href="url.json">View as JSON</a> 3481cb0ef41Sopenharmony_ci </li> 3491cb0ef41Sopenharmony_ci <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/url.md">Edit on GitHub</a></li> 3501cb0ef41Sopenharmony_ci </ul> 3511cb0ef41Sopenharmony_ci </div> 3521cb0ef41Sopenharmony_ci </li> 3531cb0ef41Sopenharmony_ci </ul> 3541cb0ef41Sopenharmony_ci </div> 3551cb0ef41Sopenharmony_ci <hr> 3561cb0ef41Sopenharmony_ci </header> 3571cb0ef41Sopenharmony_ci 3581cb0ef41Sopenharmony_ci <details id="toc" open><summary>Table of contents</summary><ul> 3591cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#url">URL</a></span> 3601cb0ef41Sopenharmony_ci<ul> 3611cb0ef41Sopenharmony_ci<li><a href="#url-strings-and-url-objects">URL strings and URL objects</a> 3621cb0ef41Sopenharmony_ci<ul> 3631cb0ef41Sopenharmony_ci<li><a href="#constructing-a-url-from-component-parts-and-getting-the-constructed-string">Constructing a URL from component parts and getting the constructed string</a></li> 3641cb0ef41Sopenharmony_ci</ul> 3651cb0ef41Sopenharmony_ci</li> 3661cb0ef41Sopenharmony_ci<li><a href="#the-whatwg-url-api">The WHATWG URL API</a> 3671cb0ef41Sopenharmony_ci<ul> 3681cb0ef41Sopenharmony_ci<li><a href="#class-url">Class: <code>URL</code></a> 3691cb0ef41Sopenharmony_ci<ul> 3701cb0ef41Sopenharmony_ci<li><a href="#new-urlinput-base"><code>new URL(input[, base])</code></a></li> 3711cb0ef41Sopenharmony_ci<li><a href="#urlhash"><code>url.hash</code></a></li> 3721cb0ef41Sopenharmony_ci<li><a href="#urlhost"><code>url.host</code></a></li> 3731cb0ef41Sopenharmony_ci<li><a href="#urlhostname"><code>url.hostname</code></a></li> 3741cb0ef41Sopenharmony_ci<li><a href="#urlhref"><code>url.href</code></a></li> 3751cb0ef41Sopenharmony_ci<li><a href="#urlorigin"><code>url.origin</code></a></li> 3761cb0ef41Sopenharmony_ci<li><a href="#urlpassword"><code>url.password</code></a></li> 3771cb0ef41Sopenharmony_ci<li><a href="#urlpathname"><code>url.pathname</code></a></li> 3781cb0ef41Sopenharmony_ci<li><a href="#urlport"><code>url.port</code></a></li> 3791cb0ef41Sopenharmony_ci<li><a href="#urlprotocol"><code>url.protocol</code></a> 3801cb0ef41Sopenharmony_ci<ul> 3811cb0ef41Sopenharmony_ci<li><a href="#special-schemes">Special schemes</a></li> 3821cb0ef41Sopenharmony_ci</ul> 3831cb0ef41Sopenharmony_ci</li> 3841cb0ef41Sopenharmony_ci<li><a href="#urlsearch"><code>url.search</code></a></li> 3851cb0ef41Sopenharmony_ci<li><a href="#urlsearchparams"><code>url.searchParams</code></a></li> 3861cb0ef41Sopenharmony_ci<li><a href="#urlusername"><code>url.username</code></a></li> 3871cb0ef41Sopenharmony_ci<li><a href="#urltostring"><code>url.toString()</code></a></li> 3881cb0ef41Sopenharmony_ci<li><a href="#urltojson"><code>url.toJSON()</code></a></li> 3891cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#urlcreateobjecturlblob"><code>URL.createObjectURL(blob)</code></a></span></li> 3901cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#urlrevokeobjecturlid"><code>URL.revokeObjectURL(id)</code></a></span></li> 3911cb0ef41Sopenharmony_ci<li><a href="#urlcanparseinput-base"><code>URL.canParse(input[, base])</code></a></li> 3921cb0ef41Sopenharmony_ci</ul> 3931cb0ef41Sopenharmony_ci</li> 3941cb0ef41Sopenharmony_ci<li><a href="#class-urlsearchparams">Class: <code>URLSearchParams</code></a> 3951cb0ef41Sopenharmony_ci<ul> 3961cb0ef41Sopenharmony_ci<li><a href="#new-urlsearchparams"><code>new URLSearchParams()</code></a></li> 3971cb0ef41Sopenharmony_ci<li><a href="#new-urlsearchparamsstring"><code>new URLSearchParams(string)</code></a></li> 3981cb0ef41Sopenharmony_ci<li><a href="#new-urlsearchparamsobj"><code>new URLSearchParams(obj)</code></a></li> 3991cb0ef41Sopenharmony_ci<li><a href="#new-urlsearchparamsiterable"><code>new URLSearchParams(iterable)</code></a></li> 4001cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamsappendname-value"><code>urlSearchParams.append(name, value)</code></a></li> 4011cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamsdeletename-value"><code>urlSearchParams.delete(name[, value])</code></a></li> 4021cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamsentries"><code>urlSearchParams.entries()</code></a></li> 4031cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamsforeachfn-thisarg"><code>urlSearchParams.forEach(fn[, thisArg])</code></a></li> 4041cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamsgetname"><code>urlSearchParams.get(name)</code></a></li> 4051cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamsgetallname"><code>urlSearchParams.getAll(name)</code></a></li> 4061cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamshasname-value"><code>urlSearchParams.has(name[, value])</code></a></li> 4071cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamskeys"><code>urlSearchParams.keys()</code></a></li> 4081cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamssetname-value"><code>urlSearchParams.set(name, value)</code></a></li> 4091cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamssize"><code>urlSearchParams.size</code></a></li> 4101cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamssort"><code>urlSearchParams.sort()</code></a></li> 4111cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamstostring"><code>urlSearchParams.toString()</code></a></li> 4121cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamsvalues"><code>urlSearchParams.values()</code></a></li> 4131cb0ef41Sopenharmony_ci<li><a href="#urlsearchparamssymboliterator"><code>urlSearchParams[Symbol.iterator]()</code></a></li> 4141cb0ef41Sopenharmony_ci</ul> 4151cb0ef41Sopenharmony_ci</li> 4161cb0ef41Sopenharmony_ci<li><a href="#urldomaintoasciidomain"><code>url.domainToASCII(domain)</code></a></li> 4171cb0ef41Sopenharmony_ci<li><a href="#urldomaintounicodedomain"><code>url.domainToUnicode(domain)</code></a></li> 4181cb0ef41Sopenharmony_ci<li><a href="#urlfileurltopathurl"><code>url.fileURLToPath(url)</code></a></li> 4191cb0ef41Sopenharmony_ci<li><a href="#urlformaturl-options"><code>url.format(URL[, options])</code></a></li> 4201cb0ef41Sopenharmony_ci<li><a href="#urlpathtofileurlpath"><code>url.pathToFileURL(path)</code></a></li> 4211cb0ef41Sopenharmony_ci<li><a href="#urlurltohttpoptionsurl"><code>url.urlToHttpOptions(url)</code></a></li> 4221cb0ef41Sopenharmony_ci</ul> 4231cb0ef41Sopenharmony_ci</li> 4241cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#legacy-url-api">Legacy URL API</a></span> 4251cb0ef41Sopenharmony_ci<ul> 4261cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#legacy-urlobject">Legacy <code>urlObject</code></a></span> 4271cb0ef41Sopenharmony_ci<ul> 4281cb0ef41Sopenharmony_ci<li><a href="#urlobjectauth"><code>urlObject.auth</code></a></li> 4291cb0ef41Sopenharmony_ci<li><a href="#urlobjecthash"><code>urlObject.hash</code></a></li> 4301cb0ef41Sopenharmony_ci<li><a href="#urlobjecthost"><code>urlObject.host</code></a></li> 4311cb0ef41Sopenharmony_ci<li><a href="#urlobjecthostname"><code>urlObject.hostname</code></a></li> 4321cb0ef41Sopenharmony_ci<li><a href="#urlobjecthref"><code>urlObject.href</code></a></li> 4331cb0ef41Sopenharmony_ci<li><a href="#urlobjectpath"><code>urlObject.path</code></a></li> 4341cb0ef41Sopenharmony_ci<li><a href="#urlobjectpathname"><code>urlObject.pathname</code></a></li> 4351cb0ef41Sopenharmony_ci<li><a href="#urlobjectport"><code>urlObject.port</code></a></li> 4361cb0ef41Sopenharmony_ci<li><a href="#urlobjectprotocol"><code>urlObject.protocol</code></a></li> 4371cb0ef41Sopenharmony_ci<li><a href="#urlobjectquery"><code>urlObject.query</code></a></li> 4381cb0ef41Sopenharmony_ci<li><a href="#urlobjectsearch"><code>urlObject.search</code></a></li> 4391cb0ef41Sopenharmony_ci<li><a href="#urlobjectslashes"><code>urlObject.slashes</code></a></li> 4401cb0ef41Sopenharmony_ci</ul> 4411cb0ef41Sopenharmony_ci</li> 4421cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#urlformaturlobject"><code>url.format(urlObject)</code></a></span></li> 4431cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse(urlString[, parseQueryString[, slashesDenoteHost]])</code></a></span></li> 4441cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#urlresolvefrom-to"><code>url.resolve(from, to)</code></a></span></li> 4451cb0ef41Sopenharmony_ci</ul> 4461cb0ef41Sopenharmony_ci</li> 4471cb0ef41Sopenharmony_ci<li><a href="#percent-encoding-in-urls">Percent-encoding in URLs</a> 4481cb0ef41Sopenharmony_ci<ul> 4491cb0ef41Sopenharmony_ci<li><a href="#legacy-api">Legacy API</a></li> 4501cb0ef41Sopenharmony_ci<li><a href="#whatwg-api">WHATWG API</a></li> 4511cb0ef41Sopenharmony_ci</ul> 4521cb0ef41Sopenharmony_ci</li> 4531cb0ef41Sopenharmony_ci</ul> 4541cb0ef41Sopenharmony_ci</li> 4551cb0ef41Sopenharmony_ci</ul></details> 4561cb0ef41Sopenharmony_ci 4571cb0ef41Sopenharmony_ci <div id="apicontent"> 4581cb0ef41Sopenharmony_ci <h2>URL<span><a class="mark" href="#url" id="url">#</a></span><a aria-hidden="true" class="legacy" id="url_url"></a></h2> 4591cb0ef41Sopenharmony_ci 4601cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 4611cb0ef41Sopenharmony_ci<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/url.js">lib/url.js</a></p> 4621cb0ef41Sopenharmony_ci<p>The <code>node:url</code> module provides utilities for URL resolution and parsing. It can 4631cb0ef41Sopenharmony_cibe accessed using:</p> 4641cb0ef41Sopenharmony_ci 4651cb0ef41Sopenharmony_ci<pre class="with-32-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> url <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>);</code><button class="copy-button">copy</button></pre> 4661cb0ef41Sopenharmony_ci<section><h3>URL strings and URL objects<span><a class="mark" href="#url-strings-and-url-objects" id="url-strings-and-url-objects">#</a></span><a aria-hidden="true" class="legacy" id="url_url_strings_and_url_objects"></a></h3> 4671cb0ef41Sopenharmony_ci<p>A URL string is a structured string containing multiple meaningful components. 4681cb0ef41Sopenharmony_ciWhen parsed, a URL object is returned containing properties for each of these 4691cb0ef41Sopenharmony_cicomponents.</p> 4701cb0ef41Sopenharmony_ci<p>The <code>node:url</code> module provides two APIs for working with URLs: a legacy API that 4711cb0ef41Sopenharmony_ciis Node.js specific, and a newer API that implements the same 4721cb0ef41Sopenharmony_ci<a href="https://url.spec.whatwg.org/">WHATWG URL Standard</a> used by web browsers.</p> 4731cb0ef41Sopenharmony_ci<p>A comparison between the WHATWG and legacy APIs is provided below. Above the URL 4741cb0ef41Sopenharmony_ci<code>'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'</code>, properties 4751cb0ef41Sopenharmony_ciof an object returned by the legacy <code>url.parse()</code> are shown. Below it are 4761cb0ef41Sopenharmony_ciproperties of a WHATWG <code>URL</code> object.</p> 4771cb0ef41Sopenharmony_ci<p>WHATWG URL's <code>origin</code> property includes <code>protocol</code> and <code>host</code>, but not 4781cb0ef41Sopenharmony_ci<code>username</code> or <code>password</code>.</p> 4791cb0ef41Sopenharmony_ci<pre><code class="language-text">┌────────────────────────────────────────────────────────────────────────────────────────────────┐ 4801cb0ef41Sopenharmony_ci│ href │ 4811cb0ef41Sopenharmony_ci├──────────┬──┬─────────────────────┬────────────────────────┬───────────────────────────┬───────┤ 4821cb0ef41Sopenharmony_ci│ protocol │ │ auth │ host │ path │ hash │ 4831cb0ef41Sopenharmony_ci│ │ │ ├─────────────────┬──────┼──────────┬────────────────┤ │ 4841cb0ef41Sopenharmony_ci│ │ │ │ hostname │ port │ pathname │ search │ │ 4851cb0ef41Sopenharmony_ci│ │ │ │ │ │ ├─┬──────────────┤ │ 4861cb0ef41Sopenharmony_ci│ │ │ │ │ │ │ │ query │ │ 4871cb0ef41Sopenharmony_ci" https: // user : pass @ sub.example.com : 8080 /p/a/t/h ? query=string #hash " 4881cb0ef41Sopenharmony_ci│ │ │ │ │ hostname │ port │ │ │ │ 4891cb0ef41Sopenharmony_ci│ │ │ │ ├─────────────────┴──────┤ │ │ │ 4901cb0ef41Sopenharmony_ci│ protocol │ │ username │ password │ host │ │ │ │ 4911cb0ef41Sopenharmony_ci├──────────┴──┼──────────┴──────────┼────────────────────────┤ │ │ │ 4921cb0ef41Sopenharmony_ci│ origin │ │ origin │ pathname │ search │ hash │ 4931cb0ef41Sopenharmony_ci├─────────────┴─────────────────────┴────────────────────────┴──────────┴────────────────┴───────┤ 4941cb0ef41Sopenharmony_ci│ href │ 4951cb0ef41Sopenharmony_ci└────────────────────────────────────────────────────────────────────────────────────────────────┘ 4961cb0ef41Sopenharmony_ci(All spaces in the "" line should be ignored. They are purely for formatting.)</code> <button class="copy-button">copy</button></pre> 4971cb0ef41Sopenharmony_ci<p>Parsing the URL string using the WHATWG API:</p> 4981cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = 4991cb0ef41Sopenharmony_ci <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'</span>);</code> <button class="copy-button">copy</button></pre> 5001cb0ef41Sopenharmony_ci<p>Parsing the URL string using the legacy API:</p> 5011cb0ef41Sopenharmony_ci 5021cb0ef41Sopenharmony_ci<pre class="with-32-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> url <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>; 5031cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myURL = 5041cb0ef41Sopenharmony_ci url.<span class="hljs-title function_">parse</span>(<span class="hljs-string">'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>); 5051cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myURL = 5061cb0ef41Sopenharmony_ci url.<span class="hljs-title function_">parse</span>(<span class="hljs-string">'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'</span>);</code><button class="copy-button">copy</button></pre> 5071cb0ef41Sopenharmony_ci<h4>Constructing a URL from component parts and getting the constructed string<span><a class="mark" href="#constructing-a-url-from-component-parts-and-getting-the-constructed-string" id="constructing-a-url-from-component-parts-and-getting-the-constructed-string">#</a></span><a aria-hidden="true" class="legacy" id="url_constructing_a_url_from_component_parts_and_getting_the_constructed_string"></a></h4> 5081cb0ef41Sopenharmony_ci<p>It is possible to construct a WHATWG URL from component parts using either the 5091cb0ef41Sopenharmony_ciproperty setters or a template literal string:</p> 5101cb0ef41Sopenharmony_ci<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>); 5111cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">pathname</span> = <span class="hljs-string">'/a/b/c'</span>; 5121cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">search</span> = <span class="hljs-string">'?d=e'</span>; 5131cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">hash</span> = <span class="hljs-string">'#fgh'</span>;</code> <button class="copy-button">copy</button></pre> 5141cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> pathname = <span class="hljs-string">'/a/b/c'</span>; 5151cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> search = <span class="hljs-string">'?d=e'</span>; 5161cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> hash = <span class="hljs-string">'#fgh'</span>; 5171cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">`https://example.org<span class="hljs-subst">${pathname}</span><span class="hljs-subst">${search}</span><span class="hljs-subst">${hash}</span>`</span>);</code> <button class="copy-button">copy</button></pre> 5181cb0ef41Sopenharmony_ci<p>To get the constructed URL string, use the <code>href</code> property accessor:</p> 5191cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);</code> <button class="copy-button">copy</button></pre> 5201cb0ef41Sopenharmony_ci</section><section><h3>The WHATWG URL API<span><a class="mark" href="#the-whatwg-url-api" id="the-whatwg-url-api">#</a></span><a aria-hidden="true" class="legacy" id="url_the_whatwg_url_api"></a></h3> 5211cb0ef41Sopenharmony_ci<h4>Class: <code>URL</code><span><a class="mark" href="#class-url" id="class-url">#</a></span><a aria-hidden="true" class="legacy" id="url_class_url"></a></h4> 5221cb0ef41Sopenharmony_ci<div class="api_metadata"> 5231cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 5241cb0ef41Sopenharmony_ci<table> 5251cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 5261cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 5271cb0ef41Sopenharmony_ci<td><p>The class is now available on the global object.</p></td></tr> 5281cb0ef41Sopenharmony_ci<tr><td>v7.0.0, v6.13.0</td> 5291cb0ef41Sopenharmony_ci<td><p><span>Added in: v7.0.0, v6.13.0</span></p></td></tr> 5301cb0ef41Sopenharmony_ci</tbody></table> 5311cb0ef41Sopenharmony_ci</details> 5321cb0ef41Sopenharmony_ci</div> 5331cb0ef41Sopenharmony_ci<p>Browser-compatible <code>URL</code> class, implemented by following the WHATWG URL 5341cb0ef41Sopenharmony_ciStandard. <a href="https://url.spec.whatwg.org/#example-url-parsing">Examples of parsed URLs</a> may be found in the Standard itself. 5351cb0ef41Sopenharmony_ciThe <code>URL</code> class is also available on the global object.</p> 5361cb0ef41Sopenharmony_ci<p>In accordance with browser conventions, all properties of <code>URL</code> objects 5371cb0ef41Sopenharmony_ciare implemented as getters and setters on the class prototype, rather than as 5381cb0ef41Sopenharmony_cidata properties on the object itself. Thus, unlike <a href="#legacy-urlobject">legacy <code>urlObject</code></a>s, 5391cb0ef41Sopenharmony_ciusing 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 5401cb0ef41Sopenharmony_cireturn <code>true</code>.</p> 5411cb0ef41Sopenharmony_ci<h5><code>new URL(input[, base])</code><span><a class="mark" href="#new-urlinput-base" id="new-urlinput-base">#</a></span><a aria-hidden="true" class="legacy" id="url_new_url_input_base"></a></h5> 5421cb0ef41Sopenharmony_ci<!-- 5431cb0ef41Sopenharmony_cichanges: 5441cb0ef41Sopenharmony_ci - version: v18.17.0 5451cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/47339 5461cb0ef41Sopenharmony_ci description: ICU requirement is removed. 5471cb0ef41Sopenharmony_ci--> 5481cb0ef41Sopenharmony_ci<ul> 5491cb0ef41Sopenharmony_ci<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> 5501cb0ef41Sopenharmony_ciis relative, then <code>base</code> is required. If <code>input</code> is absolute, the <code>base</code> 5511cb0ef41Sopenharmony_ciis ignored. If <code>input</code> is not a string, it is <a href="https://tc39.es/ecma262/#sec-tostring">converted to a string</a> first.</li> 5521cb0ef41Sopenharmony_ci<li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The base URL to resolve against if the <code>input</code> is not 5531cb0ef41Sopenharmony_ciabsolute. If <code>base</code> is not a string, it is <a href="https://tc39.es/ecma262/#sec-tostring">converted to a string</a> first.</li> 5541cb0ef41Sopenharmony_ci</ul> 5551cb0ef41Sopenharmony_ci<p>Creates a new <code>URL</code> object by parsing the <code>input</code> relative to the <code>base</code>. If 5561cb0ef41Sopenharmony_ci<code>base</code> is passed as a string, it will be parsed equivalent to <code>new URL(base)</code>.</p> 5571cb0ef41Sopenharmony_ci<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>); 5581cb0ef41Sopenharmony_ci<span class="hljs-comment">// https://example.org/foo</span></code> <button class="copy-button">copy</button></pre> 5591cb0ef41Sopenharmony_ci<p>The URL constructor is accessible as a property on the global object. 5601cb0ef41Sopenharmony_ciIt can also be imported from the built-in url module:</p> 5611cb0ef41Sopenharmony_ci 5621cb0ef41Sopenharmony_ci<pre class="with-63-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-variable constant_">URL</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>; 5631cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable constant_">URL</span> === globalThis.<span class="hljs-property">URL</span>); <span class="hljs-comment">// Prints 'true'.</span></code><code class="language-js cjs"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable constant_">URL</span> === <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>).<span class="hljs-property">URL</span>); <span class="hljs-comment">// Prints 'true'.</span></code><button class="copy-button">copy</button></pre> 5641cb0ef41Sopenharmony_ci<p>A <code>TypeError</code> will be thrown if the <code>input</code> or <code>base</code> are not valid URLs. Note 5651cb0ef41Sopenharmony_cithat an effort will be made to coerce the given values into strings. For 5661cb0ef41Sopenharmony_ciinstance:</p> 5671cb0ef41Sopenharmony_ci<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> }); 5681cb0ef41Sopenharmony_ci<span class="hljs-comment">// https://example.org/</span></code> <button class="copy-button">copy</button></pre> 5691cb0ef41Sopenharmony_ci<p>Unicode characters appearing within the host name of <code>input</code> will be 5701cb0ef41Sopenharmony_ciautomatically converted to ASCII using the <a href="https://tools.ietf.org/html/rfc5891#section-4.4">Punycode</a> algorithm.</p> 5711cb0ef41Sopenharmony_ci<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>); 5721cb0ef41Sopenharmony_ci<span class="hljs-comment">// https://xn--g6w251d/</span></code> <button class="copy-button">copy</button></pre> 5731cb0ef41Sopenharmony_ci<p>In cases where it is not known in advance if <code>input</code> is an absolute URL 5741cb0ef41Sopenharmony_ciand a <code>base</code> is provided, it is advised to validate that the <code>origin</code> of 5751cb0ef41Sopenharmony_cithe <code>URL</code> object is what is expected.</p> 5761cb0ef41Sopenharmony_ci<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>); 5771cb0ef41Sopenharmony_ci<span class="hljs-comment">// http://example.com/</span> 5781cb0ef41Sopenharmony_ci 5791cb0ef41Sopenharmony_cimyURL = <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>); 5801cb0ef41Sopenharmony_ci<span class="hljs-comment">// https://example.com/</span> 5811cb0ef41Sopenharmony_ci 5821cb0ef41Sopenharmony_cimyURL = <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>); 5831cb0ef41Sopenharmony_ci<span class="hljs-comment">// foo://Example.com/</span> 5841cb0ef41Sopenharmony_ci 5851cb0ef41Sopenharmony_cimyURL = <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>); 5861cb0ef41Sopenharmony_ci<span class="hljs-comment">// http://example.com/</span> 5871cb0ef41Sopenharmony_ci 5881cb0ef41Sopenharmony_cimyURL = <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>); 5891cb0ef41Sopenharmony_ci<span class="hljs-comment">// https://example.org/Example.com/</span> 5901cb0ef41Sopenharmony_ci 5911cb0ef41Sopenharmony_cimyURL = <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>); 5921cb0ef41Sopenharmony_ci<span class="hljs-comment">// foo:Example.com/</span></code> <button class="copy-button">copy</button></pre> 5931cb0ef41Sopenharmony_ci<h5><code>url.hash</code><span><a class="mark" href="#urlhash" id="urlhash">#</a></span><a aria-hidden="true" class="legacy" id="url_url_hash"></a></h5> 5941cb0ef41Sopenharmony_ci<ul> 5951cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 5961cb0ef41Sopenharmony_ci</ul> 5971cb0ef41Sopenharmony_ci<p>Gets and sets the fragment portion of the URL.</p> 5981cb0ef41Sopenharmony_ci<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>); 5991cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">hash</span>); 6001cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints #bar</span> 6011cb0ef41Sopenharmony_ci 6021cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">hash</span> = <span class="hljs-string">'baz'</span>; 6031cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 6041cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://example.org/foo#baz</span></code> <button class="copy-button">copy</button></pre> 6051cb0ef41Sopenharmony_ci<p>Invalid URL characters included in the value assigned to the <code>hash</code> property 6061cb0ef41Sopenharmony_ciare <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which characters to 6071cb0ef41Sopenharmony_cipercent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a> and 6081cb0ef41Sopenharmony_ci<a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p> 6091cb0ef41Sopenharmony_ci<h5><code>url.host</code><span><a class="mark" href="#urlhost" id="urlhost">#</a></span><a aria-hidden="true" class="legacy" id="url_url_host"></a></h5> 6101cb0ef41Sopenharmony_ci<ul> 6111cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 6121cb0ef41Sopenharmony_ci</ul> 6131cb0ef41Sopenharmony_ci<p>Gets and sets the host portion of the URL.</p> 6141cb0ef41Sopenharmony_ci<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>); 6151cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">host</span>); 6161cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints example.org:81</span> 6171cb0ef41Sopenharmony_ci 6181cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">host</span> = <span class="hljs-string">'example.com:82'</span>; 6191cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 6201cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://example.com:82/foo</span></code> <button class="copy-button">copy</button></pre> 6211cb0ef41Sopenharmony_ci<p>Invalid host values assigned to the <code>host</code> property are ignored.</p> 6221cb0ef41Sopenharmony_ci<h5><code>url.hostname</code><span><a class="mark" href="#urlhostname" id="urlhostname">#</a></span><a aria-hidden="true" class="legacy" id="url_url_hostname"></a></h5> 6231cb0ef41Sopenharmony_ci<ul> 6241cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 6251cb0ef41Sopenharmony_ci</ul> 6261cb0ef41Sopenharmony_ci<p>Gets and sets the host name portion of the URL. The key difference between 6271cb0ef41Sopenharmony_ci<code>url.host</code> and <code>url.hostname</code> is that <code>url.hostname</code> does <em>not</em> include the 6281cb0ef41Sopenharmony_ciport.</p> 6291cb0ef41Sopenharmony_ci<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>); 6301cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">hostname</span>); 6311cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints example.org</span> 6321cb0ef41Sopenharmony_ci 6331cb0ef41Sopenharmony_ci<span class="hljs-comment">// Setting the hostname does not change the port</span> 6341cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">hostname</span> = <span class="hljs-string">'example.com'</span>; 6351cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 6361cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://example.com:81/foo</span> 6371cb0ef41Sopenharmony_ci 6381cb0ef41Sopenharmony_ci<span class="hljs-comment">// Use myURL.host to change the hostname and port</span> 6391cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">host</span> = <span class="hljs-string">'example.org:82'</span>; 6401cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 6411cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://example.org:82/foo</span></code> <button class="copy-button">copy</button></pre> 6421cb0ef41Sopenharmony_ci<p>Invalid host name values assigned to the <code>hostname</code> property are ignored.</p> 6431cb0ef41Sopenharmony_ci<h5><code>url.href</code><span><a class="mark" href="#urlhref" id="urlhref">#</a></span><a aria-hidden="true" class="legacy" id="url_url_href"></a></h5> 6441cb0ef41Sopenharmony_ci<ul> 6451cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 6461cb0ef41Sopenharmony_ci</ul> 6471cb0ef41Sopenharmony_ci<p>Gets and sets the serialized URL.</p> 6481cb0ef41Sopenharmony_ci<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>); 6491cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 6501cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://example.org/foo</span> 6511cb0ef41Sopenharmony_ci 6521cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">href</span> = <span class="hljs-string">'https://example.com/bar'</span>; 6531cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 6541cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://example.com/bar</span></code> <button class="copy-button">copy</button></pre> 6551cb0ef41Sopenharmony_ci<p>Getting the value of the <code>href</code> property is equivalent to calling 6561cb0ef41Sopenharmony_ci<a href="#urltostring"><code>url.toString()</code></a>.</p> 6571cb0ef41Sopenharmony_ci<p>Setting the value of this property to a new value is equivalent to creating a 6581cb0ef41Sopenharmony_cinew <code>URL</code> object using <a href="#new-urlinput-base"><code>new URL(value)</code></a>. Each of the <code>URL</code> 6591cb0ef41Sopenharmony_ciobject's properties will be modified.</p> 6601cb0ef41Sopenharmony_ci<p>If the value assigned to the <code>href</code> property is not a valid URL, a <code>TypeError</code> 6611cb0ef41Sopenharmony_ciwill be thrown.</p> 6621cb0ef41Sopenharmony_ci<h5><code>url.origin</code><span><a class="mark" href="#urlorigin" id="urlorigin">#</a></span><a aria-hidden="true" class="legacy" id="url_url_origin"></a></h5> 6631cb0ef41Sopenharmony_ci<div class="api_metadata"> 6641cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 6651cb0ef41Sopenharmony_ci<table> 6661cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 6671cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 6681cb0ef41Sopenharmony_ci<td><p>The scheme "gopher" is no longer special and <code>url.origin</code> now returns <code>'null'</code> for it.</p></td></tr> 6691cb0ef41Sopenharmony_ci</tbody></table> 6701cb0ef41Sopenharmony_ci</details> 6711cb0ef41Sopenharmony_ci</div> 6721cb0ef41Sopenharmony_ci<ul> 6731cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 6741cb0ef41Sopenharmony_ci</ul> 6751cb0ef41Sopenharmony_ci<p>Gets the read-only serialization of the URL's origin.</p> 6761cb0ef41Sopenharmony_ci<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>); 6771cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">origin</span>); 6781cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://example.org</span></code> <button class="copy-button">copy</button></pre> 6791cb0ef41Sopenharmony_ci<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>); 6801cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(idnURL.<span class="hljs-property">origin</span>); 6811cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://xn--g6w251d</span> 6821cb0ef41Sopenharmony_ci 6831cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(idnURL.<span class="hljs-property">hostname</span>); 6841cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints xn--g6w251d</span></code> <button class="copy-button">copy</button></pre> 6851cb0ef41Sopenharmony_ci<h5><code>url.password</code><span><a class="mark" href="#urlpassword" id="urlpassword">#</a></span><a aria-hidden="true" class="legacy" id="url_url_password"></a></h5> 6861cb0ef41Sopenharmony_ci<ul> 6871cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 6881cb0ef41Sopenharmony_ci</ul> 6891cb0ef41Sopenharmony_ci<p>Gets and sets the password portion of the URL.</p> 6901cb0ef41Sopenharmony_ci<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>); 6911cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">password</span>); 6921cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints xyz</span> 6931cb0ef41Sopenharmony_ci 6941cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">password</span> = <span class="hljs-string">'123'</span>; 6951cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 6961cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://abc:123@example.com/</span></code> <button class="copy-button">copy</button></pre> 6971cb0ef41Sopenharmony_ci<p>Invalid URL characters included in the value assigned to the <code>password</code> property 6981cb0ef41Sopenharmony_ciare <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which characters to 6991cb0ef41Sopenharmony_cipercent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a> and 7001cb0ef41Sopenharmony_ci<a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p> 7011cb0ef41Sopenharmony_ci<h5><code>url.pathname</code><span><a class="mark" href="#urlpathname" id="urlpathname">#</a></span><a aria-hidden="true" class="legacy" id="url_url_pathname"></a></h5> 7021cb0ef41Sopenharmony_ci<ul> 7031cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 7041cb0ef41Sopenharmony_ci</ul> 7051cb0ef41Sopenharmony_ci<p>Gets and sets the path portion of the URL.</p> 7061cb0ef41Sopenharmony_ci<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>); 7071cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">pathname</span>); 7081cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints /abc/xyz</span> 7091cb0ef41Sopenharmony_ci 7101cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">pathname</span> = <span class="hljs-string">'/abcdef'</span>; 7111cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 7121cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://example.org/abcdef?123</span></code> <button class="copy-button">copy</button></pre> 7131cb0ef41Sopenharmony_ci<p>Invalid URL characters included in the value assigned to the <code>pathname</code> 7141cb0ef41Sopenharmony_ciproperty are <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which characters 7151cb0ef41Sopenharmony_cito percent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a> and 7161cb0ef41Sopenharmony_ci<a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p> 7171cb0ef41Sopenharmony_ci<h5><code>url.port</code><span><a class="mark" href="#urlport" id="urlport">#</a></span><a aria-hidden="true" class="legacy" id="url_url_port"></a></h5> 7181cb0ef41Sopenharmony_ci<div class="api_metadata"> 7191cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 7201cb0ef41Sopenharmony_ci<table> 7211cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 7221cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 7231cb0ef41Sopenharmony_ci<td><p>The scheme "gopher" is no longer special.</p></td></tr> 7241cb0ef41Sopenharmony_ci</tbody></table> 7251cb0ef41Sopenharmony_ci</details> 7261cb0ef41Sopenharmony_ci</div> 7271cb0ef41Sopenharmony_ci<ul> 7281cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 7291cb0ef41Sopenharmony_ci</ul> 7301cb0ef41Sopenharmony_ci<p>Gets and sets the port portion of the URL.</p> 7311cb0ef41Sopenharmony_ci<p>The port value may be a number or a string containing a number in the range 7321cb0ef41Sopenharmony_ci<code>0</code> to <code>65535</code> (inclusive). Setting the value to the default port of the 7331cb0ef41Sopenharmony_ci<code>URL</code> objects given <code>protocol</code> will result in the <code>port</code> value becoming 7341cb0ef41Sopenharmony_cithe empty string (<code>''</code>).</p> 7351cb0ef41Sopenharmony_ci<p>The port value can be an empty string in which case the port depends on 7361cb0ef41Sopenharmony_cithe protocol/scheme:</p> 7371cb0ef41Sopenharmony_ci 7381cb0ef41Sopenharmony_ci 7391cb0ef41Sopenharmony_ci 7401cb0ef41Sopenharmony_ci 7411cb0ef41Sopenharmony_ci 7421cb0ef41Sopenharmony_ci 7431cb0ef41Sopenharmony_ci 7441cb0ef41Sopenharmony_ci 7451cb0ef41Sopenharmony_ci 7461cb0ef41Sopenharmony_ci 7471cb0ef41Sopenharmony_ci 7481cb0ef41Sopenharmony_ci 7491cb0ef41Sopenharmony_ci 7501cb0ef41Sopenharmony_ci 7511cb0ef41Sopenharmony_ci 7521cb0ef41Sopenharmony_ci 7531cb0ef41Sopenharmony_ci 7541cb0ef41Sopenharmony_ci 7551cb0ef41Sopenharmony_ci 7561cb0ef41Sopenharmony_ci 7571cb0ef41Sopenharmony_ci 7581cb0ef41Sopenharmony_ci 7591cb0ef41Sopenharmony_ci 7601cb0ef41Sopenharmony_ci 7611cb0ef41Sopenharmony_ci 7621cb0ef41Sopenharmony_ci 7631cb0ef41Sopenharmony_ci 7641cb0ef41Sopenharmony_ci 7651cb0ef41Sopenharmony_ci 7661cb0ef41Sopenharmony_ci 7671cb0ef41Sopenharmony_ci 7681cb0ef41Sopenharmony_ci 7691cb0ef41Sopenharmony_ci 7701cb0ef41Sopenharmony_ci<table><thead><tr><th>protocol</th><th>port</th></tr></thead><tbody><tr><td>"ftp"</td><td>21</td></tr><tr><td>"file"</td><td></td></tr><tr><td>"http"</td><td>80</td></tr><tr><td>"https"</td><td>443</td></tr><tr><td>"ws"</td><td>80</td></tr><tr><td>"wss"</td><td>443</td></tr></tbody></table> 7711cb0ef41Sopenharmony_ci<p>Upon assigning a value to the port, the value will first be converted to a 7721cb0ef41Sopenharmony_cistring using <code>.toString()</code>.</p> 7731cb0ef41Sopenharmony_ci<p>If that string is invalid but it begins with a number, the leading number is 7741cb0ef41Sopenharmony_ciassigned to <code>port</code>. 7751cb0ef41Sopenharmony_ciIf the number lies outside the range denoted above, it is ignored.</p> 7761cb0ef41Sopenharmony_ci<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>); 7771cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>); 7781cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 8888</span> 7791cb0ef41Sopenharmony_ci 7801cb0ef41Sopenharmony_ci<span class="hljs-comment">// Default ports are automatically transformed to the empty string</span> 7811cb0ef41Sopenharmony_ci<span class="hljs-comment">// (HTTPS protocol's default port is 443)</span> 7821cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">port</span> = <span class="hljs-string">'443'</span>; 7831cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>); 7841cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints the empty string</span> 7851cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 7861cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://example.org/</span> 7871cb0ef41Sopenharmony_ci 7881cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">port</span> = <span class="hljs-number">1234</span>; 7891cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>); 7901cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 1234</span> 7911cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 7921cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://example.org:1234/</span> 7931cb0ef41Sopenharmony_ci 7941cb0ef41Sopenharmony_ci<span class="hljs-comment">// Completely invalid port strings are ignored</span> 7951cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">port</span> = <span class="hljs-string">'abcd'</span>; 7961cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>); 7971cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 1234</span> 7981cb0ef41Sopenharmony_ci 7991cb0ef41Sopenharmony_ci<span class="hljs-comment">// Leading numbers are treated as a port number</span> 8001cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">port</span> = <span class="hljs-string">'5678abcd'</span>; 8011cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>); 8021cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 5678</span> 8031cb0ef41Sopenharmony_ci 8041cb0ef41Sopenharmony_ci<span class="hljs-comment">// Non-integers are truncated</span> 8051cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">port</span> = <span class="hljs-number">1234.5678</span>; 8061cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>); 8071cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 1234</span> 8081cb0ef41Sopenharmony_ci 8091cb0ef41Sopenharmony_ci<span class="hljs-comment">// Out-of-range numbers which are not represented in scientific notation</span> 8101cb0ef41Sopenharmony_ci<span class="hljs-comment">// will be ignored.</span> 8111cb0ef41Sopenharmony_cimyURL.<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> 8121cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>); 8131cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 1234</span></code> <button class="copy-button">copy</button></pre> 8141cb0ef41Sopenharmony_ci<p>Numbers which contain a decimal point, 8151cb0ef41Sopenharmony_cisuch as floating-point numbers or numbers in scientific notation, 8161cb0ef41Sopenharmony_ciare not an exception to this rule. 8171cb0ef41Sopenharmony_ciLeading numbers up to the decimal point will be set as the URL's port, 8181cb0ef41Sopenharmony_ciassuming they are valid:</p> 8191cb0ef41Sopenharmony_ci<pre><code class="language-js">myURL.<span class="hljs-property">port</span> = <span class="hljs-number">4.567e21</span>; 8201cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>); 8211cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 4 (because it is the leading number in the string '4.567e21')</span></code> <button class="copy-button">copy</button></pre> 8221cb0ef41Sopenharmony_ci<h5><code>url.protocol</code><span><a class="mark" href="#urlprotocol" id="urlprotocol">#</a></span><a aria-hidden="true" class="legacy" id="url_url_protocol"></a></h5> 8231cb0ef41Sopenharmony_ci<ul> 8241cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 8251cb0ef41Sopenharmony_ci</ul> 8261cb0ef41Sopenharmony_ci<p>Gets and sets the protocol portion of the URL.</p> 8271cb0ef41Sopenharmony_ci<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>); 8281cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">protocol</span>); 8291cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https:</span> 8301cb0ef41Sopenharmony_ci 8311cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">protocol</span> = <span class="hljs-string">'ftp'</span>; 8321cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 8331cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints ftp://example.org/</span></code> <button class="copy-button">copy</button></pre> 8341cb0ef41Sopenharmony_ci<p>Invalid URL protocol values assigned to the <code>protocol</code> property are ignored.</p> 8351cb0ef41Sopenharmony_ci<h6>Special schemes<span><a class="mark" href="#special-schemes" id="special-schemes">#</a></span><a aria-hidden="true" class="legacy" id="url_special_schemes"></a></h6> 8361cb0ef41Sopenharmony_ci<div class="api_metadata"> 8371cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 8381cb0ef41Sopenharmony_ci<table> 8391cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 8401cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 8411cb0ef41Sopenharmony_ci<td><p>The scheme "gopher" is no longer special.</p></td></tr> 8421cb0ef41Sopenharmony_ci</tbody></table> 8431cb0ef41Sopenharmony_ci</details> 8441cb0ef41Sopenharmony_ci</div> 8451cb0ef41Sopenharmony_ci<p>The <a href="https://url.spec.whatwg.org/">WHATWG URL Standard</a> considers a handful of URL protocol schemes to be 8461cb0ef41Sopenharmony_ci<em>special</em> in terms of how they are parsed and serialized. When a URL is 8471cb0ef41Sopenharmony_ciparsed using one of these special protocols, the <code>url.protocol</code> property 8481cb0ef41Sopenharmony_cimay be changed to another special protocol but cannot be changed to a 8491cb0ef41Sopenharmony_cinon-special protocol, and vice versa.</p> 8501cb0ef41Sopenharmony_ci<p>For instance, changing from <code>http</code> to <code>https</code> works:</p> 8511cb0ef41Sopenharmony_ci<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>); 8521cb0ef41Sopenharmony_ciu.<span class="hljs-property">protocol</span> = <span class="hljs-string">'https'</span>; 8531cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(u.<span class="hljs-property">href</span>); 8541cb0ef41Sopenharmony_ci<span class="hljs-comment">// https://example.org/</span></code> <button class="copy-button">copy</button></pre> 8551cb0ef41Sopenharmony_ci<p>However, changing from <code>http</code> to a hypothetical <code>fish</code> protocol does not 8561cb0ef41Sopenharmony_cibecause the new protocol is not special.</p> 8571cb0ef41Sopenharmony_ci<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>); 8581cb0ef41Sopenharmony_ciu.<span class="hljs-property">protocol</span> = <span class="hljs-string">'fish'</span>; 8591cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(u.<span class="hljs-property">href</span>); 8601cb0ef41Sopenharmony_ci<span class="hljs-comment">// http://example.org/</span></code> <button class="copy-button">copy</button></pre> 8611cb0ef41Sopenharmony_ci<p>Likewise, changing from a non-special protocol to a special protocol is also 8621cb0ef41Sopenharmony_cinot permitted:</p> 8631cb0ef41Sopenharmony_ci<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>); 8641cb0ef41Sopenharmony_ciu.<span class="hljs-property">protocol</span> = <span class="hljs-string">'http'</span>; 8651cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(u.<span class="hljs-property">href</span>); 8661cb0ef41Sopenharmony_ci<span class="hljs-comment">// fish://example.org</span></code> <button class="copy-button">copy</button></pre> 8671cb0ef41Sopenharmony_ci<p>According to the WHATWG URL Standard, special protocol schemes are <code>ftp</code>, 8681cb0ef41Sopenharmony_ci<code>file</code>, <code>http</code>, <code>https</code>, <code>ws</code>, and <code>wss</code>.</p> 8691cb0ef41Sopenharmony_ci<h5><code>url.search</code><span><a class="mark" href="#urlsearch" id="urlsearch">#</a></span><a aria-hidden="true" class="legacy" id="url_url_search"></a></h5> 8701cb0ef41Sopenharmony_ci<ul> 8711cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 8721cb0ef41Sopenharmony_ci</ul> 8731cb0ef41Sopenharmony_ci<p>Gets and sets the serialized query portion of the URL.</p> 8741cb0ef41Sopenharmony_ci<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>); 8751cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">search</span>); 8761cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints ?123</span> 8771cb0ef41Sopenharmony_ci 8781cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">search</span> = <span class="hljs-string">'abc=xyz'</span>; 8791cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 8801cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://example.org/abc?abc=xyz</span></code> <button class="copy-button">copy</button></pre> 8811cb0ef41Sopenharmony_ci<p>Any invalid URL characters appearing in the value assigned the <code>search</code> 8821cb0ef41Sopenharmony_ciproperty will be <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which 8831cb0ef41Sopenharmony_cicharacters to percent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a> 8841cb0ef41Sopenharmony_ciand <a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p> 8851cb0ef41Sopenharmony_ci<h5><code>url.searchParams</code><span><a class="mark" href="#urlsearchparams" id="urlsearchparams">#</a></span><a aria-hidden="true" class="legacy" id="url_url_searchparams"></a></h5> 8861cb0ef41Sopenharmony_ci<ul> 8871cb0ef41Sopenharmony_ci<li><a href="url.html#class-urlsearchparams" class="type"><URLSearchParams></a></li> 8881cb0ef41Sopenharmony_ci</ul> 8891cb0ef41Sopenharmony_ci<p>Gets the <a href="#class-urlsearchparams"><code>URLSearchParams</code></a> object representing the query parameters of the 8901cb0ef41Sopenharmony_ciURL. This property is read-only but the <code>URLSearchParams</code> object it provides 8911cb0ef41Sopenharmony_cican be used to mutate the URL instance; to replace the entirety of query 8921cb0ef41Sopenharmony_ciparameters of the URL, use the <a href="#urlsearch"><code>url.search</code></a> setter. See 8931cb0ef41Sopenharmony_ci<a href="#class-urlsearchparams"><code>URLSearchParams</code></a> documentation for details.</p> 8941cb0ef41Sopenharmony_ci<p>Use care when using <code>.searchParams</code> to modify the <code>URL</code> because, 8951cb0ef41Sopenharmony_ciper the WHATWG specification, the <code>URLSearchParams</code> object uses 8961cb0ef41Sopenharmony_cidifferent rules to determine which characters to percent-encode. For 8971cb0ef41Sopenharmony_ciinstance, the <code>URL</code> object will not percent encode the ASCII tilde (<code>~</code>) 8981cb0ef41Sopenharmony_cicharacter, while <code>URLSearchParams</code> will always encode it:</p> 8991cb0ef41Sopenharmony_ci<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>); 9001cb0ef41Sopenharmony_ci 9011cb0ef41Sopenharmony_ci<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> 9021cb0ef41Sopenharmony_ci 9031cb0ef41Sopenharmony_ci<span class="hljs-comment">// Modify the URL via searchParams...</span> 9041cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">sort</span>(); 9051cb0ef41Sopenharmony_ci 9061cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">search</span>); <span class="hljs-comment">// prints ?foo=%7Ebar</span></code> <button class="copy-button">copy</button></pre> 9071cb0ef41Sopenharmony_ci<h5><code>url.username</code><span><a class="mark" href="#urlusername" id="urlusername">#</a></span><a aria-hidden="true" class="legacy" id="url_url_username"></a></h5> 9081cb0ef41Sopenharmony_ci<ul> 9091cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 9101cb0ef41Sopenharmony_ci</ul> 9111cb0ef41Sopenharmony_ci<p>Gets and sets the username portion of the URL.</p> 9121cb0ef41Sopenharmony_ci<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>); 9131cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">username</span>); 9141cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints abc</span> 9151cb0ef41Sopenharmony_ci 9161cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">username</span> = <span class="hljs-string">'123'</span>; 9171cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 9181cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://123:xyz@example.com/</span></code> <button class="copy-button">copy</button></pre> 9191cb0ef41Sopenharmony_ci<p>Any invalid URL characters appearing in the value assigned the <code>username</code> 9201cb0ef41Sopenharmony_ciproperty will be <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which 9211cb0ef41Sopenharmony_cicharacters to percent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a> 9221cb0ef41Sopenharmony_ciand <a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p> 9231cb0ef41Sopenharmony_ci<h5><code>url.toString()</code><span><a class="mark" href="#urltostring" id="urltostring">#</a></span><a aria-hidden="true" class="legacy" id="url_url_tostring"></a></h5> 9241cb0ef41Sopenharmony_ci<ul> 9251cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 9261cb0ef41Sopenharmony_ci</ul> 9271cb0ef41Sopenharmony_ci<p>The <code>toString()</code> method on the <code>URL</code> object returns the serialized URL. The 9281cb0ef41Sopenharmony_civalue returned is equivalent to that of <a href="#urlhref"><code>url.href</code></a> and <a href="#urltojson"><code>url.toJSON()</code></a>.</p> 9291cb0ef41Sopenharmony_ci<h5><code>url.toJSON()</code><span><a class="mark" href="#urltojson" id="urltojson">#</a></span><a aria-hidden="true" class="legacy" id="url_url_tojson"></a></h5> 9301cb0ef41Sopenharmony_ci<ul> 9311cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 9321cb0ef41Sopenharmony_ci</ul> 9331cb0ef41Sopenharmony_ci<p>The <code>toJSON()</code> method on the <code>URL</code> object returns the serialized URL. The 9341cb0ef41Sopenharmony_civalue returned is equivalent to that of <a href="#urlhref"><code>url.href</code></a> and 9351cb0ef41Sopenharmony_ci<a href="#urltostring"><code>url.toString()</code></a>.</p> 9361cb0ef41Sopenharmony_ci<p>This method is automatically called when an <code>URL</code> object is serialized 9371cb0ef41Sopenharmony_ciwith <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify"><code>JSON.stringify()</code></a>.</p> 9381cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> myURLs = [ 9391cb0ef41Sopenharmony_ci <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://www.example.com'</span>), 9401cb0ef41Sopenharmony_ci <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://test.example.org'</span>), 9411cb0ef41Sopenharmony_ci]; 9421cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(myURLs)); 9431cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints ["https://www.example.com/","https://test.example.org/"]</span></code> <button class="copy-button">copy</button></pre> 9441cb0ef41Sopenharmony_ci<h5><code>URL.createObjectURL(blob)</code><span><a class="mark" href="#urlcreateobjecturlblob" id="urlcreateobjecturlblob">#</a></span><a aria-hidden="true" class="legacy" id="url_url_createobjecturl_blob"></a></h5> 9451cb0ef41Sopenharmony_ci<div class="api_metadata"> 9461cb0ef41Sopenharmony_ci<span>Added in: v16.7.0</span> 9471cb0ef41Sopenharmony_ci</div> 9481cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 9491cb0ef41Sopenharmony_ci<ul> 9501cb0ef41Sopenharmony_ci<li><code>blob</code> <a href="buffer.html#class-blob" class="type"><Blob></a></li> 9511cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 9521cb0ef41Sopenharmony_ci</ul> 9531cb0ef41Sopenharmony_ci<p>Creates a <code>'blob:nodedata:...'</code> URL string that represents the given <a href="buffer.html#class-blob" class="type"><Blob></a> 9541cb0ef41Sopenharmony_ciobject and can be used to retrieve the <code>Blob</code> later.</p> 9551cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> { 9561cb0ef41Sopenharmony_ci <span class="hljs-title class_">Blob</span>, 9571cb0ef41Sopenharmony_ci resolveObjectURL, 9581cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>); 9591cb0ef41Sopenharmony_ci 9601cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> blob = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Blob</span>([<span class="hljs-string">'hello'</span>]); 9611cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> id = <span class="hljs-variable constant_">URL</span>.<span class="hljs-title function_">createObjectURL</span>(blob); 9621cb0ef41Sopenharmony_ci 9631cb0ef41Sopenharmony_ci<span class="hljs-comment">// later...</span> 9641cb0ef41Sopenharmony_ci 9651cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> otherBlob = <span class="hljs-title function_">resolveObjectURL</span>(id); 9661cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(otherBlob.<span class="hljs-property">size</span>);</code> <button class="copy-button">copy</button></pre> 9671cb0ef41Sopenharmony_ci<p>The data stored by the registered <a href="buffer.html#class-blob" class="type"><Blob></a> will be retained in memory until 9681cb0ef41Sopenharmony_ci<code>URL.revokeObjectURL()</code> is called to remove it.</p> 9691cb0ef41Sopenharmony_ci<p><code>Blob</code> objects are registered within the current thread. If using Worker 9701cb0ef41Sopenharmony_ciThreads, <code>Blob</code> objects registered within one Worker will not be available 9711cb0ef41Sopenharmony_cito other workers or the main thread.</p> 9721cb0ef41Sopenharmony_ci<h5><code>URL.revokeObjectURL(id)</code><span><a class="mark" href="#urlrevokeobjecturlid" id="urlrevokeobjecturlid">#</a></span><a aria-hidden="true" class="legacy" id="url_url_revokeobjecturl_id"></a></h5> 9731cb0ef41Sopenharmony_ci<div class="api_metadata"> 9741cb0ef41Sopenharmony_ci<span>Added in: v16.7.0</span> 9751cb0ef41Sopenharmony_ci</div> 9761cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 9771cb0ef41Sopenharmony_ci<ul> 9781cb0ef41Sopenharmony_ci<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A <code>'blob:nodedata:...</code> URL string returned by a prior call to 9791cb0ef41Sopenharmony_ci<code>URL.createObjectURL()</code>.</li> 9801cb0ef41Sopenharmony_ci</ul> 9811cb0ef41Sopenharmony_ci<p>Removes the stored <a href="buffer.html#class-blob" class="type"><Blob></a> identified by the given ID. Attempting to revoke a 9821cb0ef41Sopenharmony_ciID that isn't registered will silently fail.</p> 9831cb0ef41Sopenharmony_ci<h5><code>URL.canParse(input[, base])</code><span><a class="mark" href="#urlcanparseinput-base" id="urlcanparseinput-base">#</a></span><a aria-hidden="true" class="legacy" id="url_url_canparse_input_base"></a></h5> 9841cb0ef41Sopenharmony_ci<div class="api_metadata"> 9851cb0ef41Sopenharmony_ci<span>Added in: v18.17.0</span> 9861cb0ef41Sopenharmony_ci</div> 9871cb0ef41Sopenharmony_ci<ul> 9881cb0ef41Sopenharmony_ci<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> 9891cb0ef41Sopenharmony_ciis relative, then <code>base</code> is required. If <code>input</code> is absolute, the <code>base</code> 9901cb0ef41Sopenharmony_ciis ignored. If <code>input</code> is not a string, it is <a href="https://tc39.es/ecma262/#sec-tostring">converted to a string</a> first.</li> 9911cb0ef41Sopenharmony_ci<li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The base URL to resolve against if the <code>input</code> is not 9921cb0ef41Sopenharmony_ciabsolute. If <code>base</code> is not a string, it is <a href="https://tc39.es/ecma262/#sec-tostring">converted to a string</a> first.</li> 9931cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 9941cb0ef41Sopenharmony_ci</ul> 9951cb0ef41Sopenharmony_ci<p>Checks if an <code>input</code> relative to the <code>base</code> can be parsed to a <code>URL</code>.</p> 9961cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> isValid = <span class="hljs-variable constant_">URL</span>.<span class="hljs-title function_">canParse</span>(<span class="hljs-string">'/foo'</span>, <span class="hljs-string">'https://example.org/'</span>); <span class="hljs-comment">// true</span> 9971cb0ef41Sopenharmony_ci 9981cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> isNotValid = <span class="hljs-variable constant_">URL</span>.<span class="hljs-title function_">canParse</span>(<span class="hljs-string">'/foo'</span>); <span class="hljs-comment">// false</span></code> <button class="copy-button">copy</button></pre> 9991cb0ef41Sopenharmony_ci<h4>Class: <code>URLSearchParams</code><span><a class="mark" href="#class-urlsearchparams" id="class-urlsearchparams">#</a></span><a aria-hidden="true" class="legacy" id="url_class_urlsearchparams"></a></h4> 10001cb0ef41Sopenharmony_ci<div class="api_metadata"> 10011cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 10021cb0ef41Sopenharmony_ci<table> 10031cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 10041cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 10051cb0ef41Sopenharmony_ci<td><p>The class is now available on the global object.</p></td></tr> 10061cb0ef41Sopenharmony_ci<tr><td>v7.5.0, v6.13.0</td> 10071cb0ef41Sopenharmony_ci<td><p><span>Added in: v7.5.0, v6.13.0</span></p></td></tr> 10081cb0ef41Sopenharmony_ci</tbody></table> 10091cb0ef41Sopenharmony_ci</details> 10101cb0ef41Sopenharmony_ci</div> 10111cb0ef41Sopenharmony_ci<p>The <code>URLSearchParams</code> API provides read and write access to the query of a 10121cb0ef41Sopenharmony_ci<code>URL</code>. The <code>URLSearchParams</code> class can also be used standalone with one of the 10131cb0ef41Sopenharmony_cifour following constructors. 10141cb0ef41Sopenharmony_ciThe <code>URLSearchParams</code> class is also available on the global object.</p> 10151cb0ef41Sopenharmony_ci<p>The WHATWG <code>URLSearchParams</code> interface and the <a href="querystring.html"><code>querystring</code></a> module have 10161cb0ef41Sopenharmony_cisimilar purpose, but the purpose of the <a href="querystring.html"><code>querystring</code></a> module is more 10171cb0ef41Sopenharmony_cigeneral, as it allows the customization of delimiter characters (<code>&</code> and <code>=</code>). 10181cb0ef41Sopenharmony_ciOn the other hand, this API is designed purely for URL query strings.</p> 10191cb0ef41Sopenharmony_ci<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>); 10201cb0ef41Sopenharmony_ci<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>)); 10211cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 123</span> 10221cb0ef41Sopenharmony_ci 10231cb0ef41Sopenharmony_cimyURL.<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>); 10241cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 10251cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://example.org/?abc=123&abc=xyz</span> 10261cb0ef41Sopenharmony_ci 10271cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">delete</span>(<span class="hljs-string">'abc'</span>); 10281cb0ef41Sopenharmony_cimyURL.<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>); 10291cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 10301cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://example.org/?a=b</span> 10311cb0ef41Sopenharmony_ci 10321cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> newSearchParams = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(myURL.<span class="hljs-property">searchParams</span>); 10331cb0ef41Sopenharmony_ci<span class="hljs-comment">// The above is equivalent to</span> 10341cb0ef41Sopenharmony_ci<span class="hljs-comment">// const newSearchParams = new URLSearchParams(myURL.search);</span> 10351cb0ef41Sopenharmony_ci 10361cb0ef41Sopenharmony_cinewSearchParams.<span class="hljs-title function_">append</span>(<span class="hljs-string">'a'</span>, <span class="hljs-string">'c'</span>); 10371cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 10381cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://example.org/?a=b</span> 10391cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(newSearchParams.<span class="hljs-title function_">toString</span>()); 10401cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints a=b&a=c</span> 10411cb0ef41Sopenharmony_ci 10421cb0ef41Sopenharmony_ci<span class="hljs-comment">// newSearchParams.toString() is implicitly called</span> 10431cb0ef41Sopenharmony_cimyURL.<span class="hljs-property">search</span> = newSearchParams; 10441cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 10451cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://example.org/?a=b&a=c</span> 10461cb0ef41Sopenharmony_cinewSearchParams.<span class="hljs-title function_">delete</span>(<span class="hljs-string">'a'</span>); 10471cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 10481cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://example.org/?a=b&a=c</span></code> <button class="copy-button">copy</button></pre> 10491cb0ef41Sopenharmony_ci<h5><code>new URLSearchParams()</code><span><a class="mark" href="#new-urlsearchparams" id="new-urlsearchparams">#</a></span><a aria-hidden="true" class="legacy" id="url_new_urlsearchparams"></a></h5> 10501cb0ef41Sopenharmony_ci<p>Instantiate a new empty <code>URLSearchParams</code> object.</p> 10511cb0ef41Sopenharmony_ci<h5><code>new URLSearchParams(string)</code><span><a class="mark" href="#new-urlsearchparamsstring" id="new-urlsearchparamsstring">#</a></span><a aria-hidden="true" class="legacy" id="url_new_urlsearchparams_string"></a></h5> 10521cb0ef41Sopenharmony_ci<ul> 10531cb0ef41Sopenharmony_ci<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> 10541cb0ef41Sopenharmony_ci</ul> 10551cb0ef41Sopenharmony_ci<p>Parse the <code>string</code> as a query string, and use it to instantiate a new 10561cb0ef41Sopenharmony_ci<code>URLSearchParams</code> object. A leading <code>'?'</code>, if present, is ignored.</p> 10571cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">let</span> params; 10581cb0ef41Sopenharmony_ci 10591cb0ef41Sopenharmony_ciparams = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(<span class="hljs-string">'user=abc&query=xyz'</span>); 10601cb0ef41Sopenharmony_ci<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>)); 10611cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 'abc'</span> 10621cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>()); 10631cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 'user=abc&query=xyz'</span> 10641cb0ef41Sopenharmony_ci 10651cb0ef41Sopenharmony_ciparams = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(<span class="hljs-string">'?user=abc&query=xyz'</span>); 10661cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>()); 10671cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 'user=abc&query=xyz'</span></code> <button class="copy-button">copy</button></pre> 10681cb0ef41Sopenharmony_ci<h5><code>new URLSearchParams(obj)</code><span><a class="mark" href="#new-urlsearchparamsobj" id="new-urlsearchparamsobj">#</a></span><a aria-hidden="true" class="legacy" id="url_new_urlsearchparams_obj"></a></h5> 10691cb0ef41Sopenharmony_ci<div class="api_metadata"> 10701cb0ef41Sopenharmony_ci<span>Added in: v7.10.0, v6.13.0</span> 10711cb0ef41Sopenharmony_ci</div> 10721cb0ef41Sopenharmony_ci<ul> 10731cb0ef41Sopenharmony_ci<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> 10741cb0ef41Sopenharmony_ci</ul> 10751cb0ef41Sopenharmony_ci<p>Instantiate a new <code>URLSearchParams</code> object with a query hash map. The key and 10761cb0ef41Sopenharmony_civalue of each property of <code>obj</code> are always coerced to strings.</p> 10771cb0ef41Sopenharmony_ci<p>Unlike <a href="querystring.html"><code>querystring</code></a> module, duplicate keys in the form of array values are 10781cb0ef41Sopenharmony_cinot 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 10791cb0ef41Sopenharmony_cijoins all array elements with commas.</p> 10801cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>({ 10811cb0ef41Sopenharmony_ci <span class="hljs-attr">user</span>: <span class="hljs-string">'abc'</span>, 10821cb0ef41Sopenharmony_ci <span class="hljs-attr">query</span>: [<span class="hljs-string">'first'</span>, <span class="hljs-string">'second'</span>], 10831cb0ef41Sopenharmony_ci}); 10841cb0ef41Sopenharmony_ci<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>)); 10851cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints [ 'first,second' ]</span> 10861cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>()); 10871cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 'user=abc&query=first%2Csecond'</span></code> <button class="copy-button">copy</button></pre> 10881cb0ef41Sopenharmony_ci<h5><code>new URLSearchParams(iterable)</code><span><a class="mark" href="#new-urlsearchparamsiterable" id="new-urlsearchparamsiterable">#</a></span><a aria-hidden="true" class="legacy" id="url_new_urlsearchparams_iterable"></a></h5> 10891cb0ef41Sopenharmony_ci<div class="api_metadata"> 10901cb0ef41Sopenharmony_ci<span>Added in: v7.10.0, v6.13.0</span> 10911cb0ef41Sopenharmony_ci</div> 10921cb0ef41Sopenharmony_ci<ul> 10931cb0ef41Sopenharmony_ci<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> 10941cb0ef41Sopenharmony_ci</ul> 10951cb0ef41Sopenharmony_ci<p>Instantiate a new <code>URLSearchParams</code> object with an iterable map in a way that 10961cb0ef41Sopenharmony_ciis 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 10971cb0ef41Sopenharmony_ciiterable object. That means <code>iterable</code> can be another <code>URLSearchParams</code>, in 10981cb0ef41Sopenharmony_ciwhich case the constructor will simply create a clone of the provided 10991cb0ef41Sopenharmony_ci<code>URLSearchParams</code>. Elements of <code>iterable</code> are key-value pairs, and can 11001cb0ef41Sopenharmony_cithemselves be any iterable object.</p> 11011cb0ef41Sopenharmony_ci<p>Duplicate keys are allowed.</p> 11021cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">let</span> params; 11031cb0ef41Sopenharmony_ci 11041cb0ef41Sopenharmony_ci<span class="hljs-comment">// Using an array</span> 11051cb0ef41Sopenharmony_ciparams = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>([ 11061cb0ef41Sopenharmony_ci [<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>], 11071cb0ef41Sopenharmony_ci [<span class="hljs-string">'query'</span>, <span class="hljs-string">'first'</span>], 11081cb0ef41Sopenharmony_ci [<span class="hljs-string">'query'</span>, <span class="hljs-string">'second'</span>], 11091cb0ef41Sopenharmony_ci]); 11101cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>()); 11111cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 'user=abc&query=first&query=second'</span> 11121cb0ef41Sopenharmony_ci 11131cb0ef41Sopenharmony_ci<span class="hljs-comment">// Using a Map object</span> 11141cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> map = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>(); 11151cb0ef41Sopenharmony_cimap.<span class="hljs-title function_">set</span>(<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>); 11161cb0ef41Sopenharmony_cimap.<span class="hljs-title function_">set</span>(<span class="hljs-string">'query'</span>, <span class="hljs-string">'xyz'</span>); 11171cb0ef41Sopenharmony_ciparams = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(map); 11181cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>()); 11191cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 'user=abc&query=xyz'</span> 11201cb0ef41Sopenharmony_ci 11211cb0ef41Sopenharmony_ci<span class="hljs-comment">// Using a generator function</span> 11221cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span>* <span class="hljs-title function_">getQueryPairs</span>(<span class="hljs-params"></span>) { 11231cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> [<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>]; 11241cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> [<span class="hljs-string">'query'</span>, <span class="hljs-string">'first'</span>]; 11251cb0ef41Sopenharmony_ci <span class="hljs-keyword">yield</span> [<span class="hljs-string">'query'</span>, <span class="hljs-string">'second'</span>]; 11261cb0ef41Sopenharmony_ci} 11271cb0ef41Sopenharmony_ciparams = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(<span class="hljs-title function_">getQueryPairs</span>()); 11281cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>()); 11291cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 'user=abc&query=first&query=second'</span> 11301cb0ef41Sopenharmony_ci 11311cb0ef41Sopenharmony_ci<span class="hljs-comment">// Each key-value pair must have exactly two elements</span> 11321cb0ef41Sopenharmony_ci<span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>([ 11331cb0ef41Sopenharmony_ci [<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>, <span class="hljs-string">'error'</span>], 11341cb0ef41Sopenharmony_ci]); 11351cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws TypeError [ERR_INVALID_TUPLE]:</span> 11361cb0ef41Sopenharmony_ci<span class="hljs-comment">// Each query pair must be an iterable [name, value] tuple</span></code> <button class="copy-button">copy</button></pre> 11371cb0ef41Sopenharmony_ci<h5><code>urlSearchParams.append(name, value)</code><span><a class="mark" href="#urlsearchparamsappendname-value" id="urlsearchparamsappendname-value">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_append_name_value"></a></h5> 11381cb0ef41Sopenharmony_ci<ul> 11391cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 11401cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 11411cb0ef41Sopenharmony_ci</ul> 11421cb0ef41Sopenharmony_ci<p>Append a new name-value pair to the query string.</p> 11431cb0ef41Sopenharmony_ci<h5><code>urlSearchParams.delete(name[, value])</code><span><a class="mark" href="#urlsearchparamsdeletename-value" id="urlsearchparamsdeletename-value">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_delete_name_value"></a></h5> 11441cb0ef41Sopenharmony_ci<div class="api_metadata"> 11451cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 11461cb0ef41Sopenharmony_ci<table> 11471cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 11481cb0ef41Sopenharmony_ci<tr><td>v18.18.0</td> 11491cb0ef41Sopenharmony_ci<td><p>Add support for optional <code>value</code> argument.</p></td></tr> 11501cb0ef41Sopenharmony_ci</tbody></table> 11511cb0ef41Sopenharmony_ci</details> 11521cb0ef41Sopenharmony_ci</div> 11531cb0ef41Sopenharmony_ci<ul> 11541cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 11551cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 11561cb0ef41Sopenharmony_ci</ul> 11571cb0ef41Sopenharmony_ci<p>If <code>value</code> is provided, removes all name-value pairs 11581cb0ef41Sopenharmony_ciwhere name is <code>name</code> and value is <code>value</code>..</p> 11591cb0ef41Sopenharmony_ci<p>If <code>value</code> is not provided, removes all name-value pairs whose name is <code>name</code>.</p> 11601cb0ef41Sopenharmony_ci<h5><code>urlSearchParams.entries()</code><span><a class="mark" href="#urlsearchparamsentries" id="urlsearchparamsentries">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_entries"></a></h5> 11611cb0ef41Sopenharmony_ci<ul> 11621cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 11631cb0ef41Sopenharmony_ci</ul> 11641cb0ef41Sopenharmony_ci<p>Returns an ES6 <code>Iterator</code> over each of the name-value pairs in the query. 11651cb0ef41Sopenharmony_ciEach item of the iterator is a JavaScript <code>Array</code>. The first item of the <code>Array</code> 11661cb0ef41Sopenharmony_ciis the <code>name</code>, the second item of the <code>Array</code> is the <code>value</code>.</p> 11671cb0ef41Sopenharmony_ci<p>Alias for <a href="#urlsearchparamssymboliterator"><code>urlSearchParams[@@iterator]()</code></a>.</p> 11681cb0ef41Sopenharmony_ci<h5><code>urlSearchParams.forEach(fn[, thisArg])</code><span><a class="mark" href="#urlsearchparamsforeachfn-thisarg" id="urlsearchparamsforeachfn-thisarg">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_foreach_fn_thisarg"></a></h5> 11691cb0ef41Sopenharmony_ci<div class="api_metadata"> 11701cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 11711cb0ef41Sopenharmony_ci<table> 11721cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 11731cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 11741cb0ef41Sopenharmony_ci<td><p>Passing an invalid callback to the <code>fn</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 11751cb0ef41Sopenharmony_ci</tbody></table> 11761cb0ef41Sopenharmony_ci</details> 11771cb0ef41Sopenharmony_ci</div> 11781cb0ef41Sopenharmony_ci<ul> 11791cb0ef41Sopenharmony_ci<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> 11801cb0ef41Sopenharmony_ci<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> 11811cb0ef41Sopenharmony_ci</ul> 11821cb0ef41Sopenharmony_ci<p>Iterates over each name-value pair in the query and invokes the given function.</p> 11831cb0ef41Sopenharmony_ci<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>); 11841cb0ef41Sopenharmony_cimyURL.<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> { 11851cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name, value, myURL.<span class="hljs-property">searchParams</span> === searchParams); 11861cb0ef41Sopenharmony_ci}); 11871cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 11881cb0ef41Sopenharmony_ci<span class="hljs-comment">// a b true</span> 11891cb0ef41Sopenharmony_ci<span class="hljs-comment">// c d true</span></code> <button class="copy-button">copy</button></pre> 11901cb0ef41Sopenharmony_ci<h5><code>urlSearchParams.get(name)</code><span><a class="mark" href="#urlsearchparamsgetname" id="urlsearchparamsgetname">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_get_name"></a></h5> 11911cb0ef41Sopenharmony_ci<ul> 11921cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 11931cb0ef41Sopenharmony_ci<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 11941cb0ef41Sopenharmony_ci<code>name</code>.</li> 11951cb0ef41Sopenharmony_ci</ul> 11961cb0ef41Sopenharmony_ci<p>Returns the value of the first name-value pair whose name is <code>name</code>. If there 11971cb0ef41Sopenharmony_ciare no such pairs, <code>null</code> is returned.</p> 11981cb0ef41Sopenharmony_ci<h5><code>urlSearchParams.getAll(name)</code><span><a class="mark" href="#urlsearchparamsgetallname" id="urlsearchparamsgetallname">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_getall_name"></a></h5> 11991cb0ef41Sopenharmony_ci<ul> 12001cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 12011cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 12021cb0ef41Sopenharmony_ci</ul> 12031cb0ef41Sopenharmony_ci<p>Returns the values of all name-value pairs whose name is <code>name</code>. If there are 12041cb0ef41Sopenharmony_cino such pairs, an empty array is returned.</p> 12051cb0ef41Sopenharmony_ci<h5><code>urlSearchParams.has(name[, value])</code><span><a class="mark" href="#urlsearchparamshasname-value" id="urlsearchparamshasname-value">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_has_name_value"></a></h5> 12061cb0ef41Sopenharmony_ci<div class="api_metadata"> 12071cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 12081cb0ef41Sopenharmony_ci<table> 12091cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 12101cb0ef41Sopenharmony_ci<tr><td>v18.18.0</td> 12111cb0ef41Sopenharmony_ci<td><p>Add support for optional <code>value</code> argument.</p></td></tr> 12121cb0ef41Sopenharmony_ci</tbody></table> 12131cb0ef41Sopenharmony_ci</details> 12141cb0ef41Sopenharmony_ci</div> 12151cb0ef41Sopenharmony_ci<ul> 12161cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 12171cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 12181cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 12191cb0ef41Sopenharmony_ci</ul> 12201cb0ef41Sopenharmony_ci<p>Checks if the <code>URLSearchParams</code> object contains key-value pair(s) based on 12211cb0ef41Sopenharmony_ci<code>name</code> and an optional <code>value</code> argument.</p> 12221cb0ef41Sopenharmony_ci<p>If <code>value</code> is provided, returns <code>true</code> when name-value pair with 12231cb0ef41Sopenharmony_cisame <code>name</code> and <code>value</code> exists.</p> 12241cb0ef41Sopenharmony_ci<p>If <code>value</code> is not provided, returns <code>true</code> if there is at least one name-value 12251cb0ef41Sopenharmony_cipair whose name is <code>name</code>.</p> 12261cb0ef41Sopenharmony_ci<h5><code>urlSearchParams.keys()</code><span><a class="mark" href="#urlsearchparamskeys" id="urlsearchparamskeys">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_keys"></a></h5> 12271cb0ef41Sopenharmony_ci<ul> 12281cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 12291cb0ef41Sopenharmony_ci</ul> 12301cb0ef41Sopenharmony_ci<p>Returns an ES6 <code>Iterator</code> over the names of each name-value pair.</p> 12311cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(<span class="hljs-string">'foo=bar&foo=baz'</span>); 12321cb0ef41Sopenharmony_ci<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>()) { 12331cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name); 12341cb0ef41Sopenharmony_ci} 12351cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 12361cb0ef41Sopenharmony_ci<span class="hljs-comment">// foo</span> 12371cb0ef41Sopenharmony_ci<span class="hljs-comment">// foo</span></code> <button class="copy-button">copy</button></pre> 12381cb0ef41Sopenharmony_ci<h5><code>urlSearchParams.set(name, value)</code><span><a class="mark" href="#urlsearchparamssetname-value" id="urlsearchparamssetname-value">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_set_name_value"></a></h5> 12391cb0ef41Sopenharmony_ci<ul> 12401cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 12411cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 12421cb0ef41Sopenharmony_ci</ul> 12431cb0ef41Sopenharmony_ci<p>Sets the value in the <code>URLSearchParams</code> object associated with <code>name</code> to 12441cb0ef41Sopenharmony_ci<code>value</code>. If there are any pre-existing name-value pairs whose names are <code>name</code>, 12451cb0ef41Sopenharmony_ciset the first such pair's value to <code>value</code> and remove all others. If not, 12461cb0ef41Sopenharmony_ciappend the name-value pair to the query string.</p> 12471cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(); 12481cb0ef41Sopenharmony_ciparams.<span class="hljs-title function_">append</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>); 12491cb0ef41Sopenharmony_ciparams.<span class="hljs-title function_">append</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'baz'</span>); 12501cb0ef41Sopenharmony_ciparams.<span class="hljs-title function_">append</span>(<span class="hljs-string">'abc'</span>, <span class="hljs-string">'def'</span>); 12511cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>()); 12521cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints foo=bar&foo=baz&abc=def</span> 12531cb0ef41Sopenharmony_ci 12541cb0ef41Sopenharmony_ciparams.<span class="hljs-title function_">set</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'def'</span>); 12551cb0ef41Sopenharmony_ciparams.<span class="hljs-title function_">set</span>(<span class="hljs-string">'xyz'</span>, <span class="hljs-string">'opq'</span>); 12561cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>()); 12571cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints foo=def&abc=def&xyz=opq</span></code> <button class="copy-button">copy</button></pre> 12581cb0ef41Sopenharmony_ci<h5><code>urlSearchParams.size</code><span><a class="mark" href="#urlsearchparamssize" id="urlsearchparamssize">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_size"></a></h5> 12591cb0ef41Sopenharmony_ci<div class="api_metadata"> 12601cb0ef41Sopenharmony_ci<span>Added in: v18.16.0</span> 12611cb0ef41Sopenharmony_ci</div> 12621cb0ef41Sopenharmony_ci<p>The total number of parameter entries.</p> 12631cb0ef41Sopenharmony_ci<h5><code>urlSearchParams.sort()</code><span><a class="mark" href="#urlsearchparamssort" id="urlsearchparamssort">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_sort"></a></h5> 12641cb0ef41Sopenharmony_ci<div class="api_metadata"> 12651cb0ef41Sopenharmony_ci<span>Added in: v7.7.0, v6.13.0</span> 12661cb0ef41Sopenharmony_ci</div> 12671cb0ef41Sopenharmony_ci<p>Sort all existing name-value pairs in-place by their names. Sorting is done 12681cb0ef41Sopenharmony_ciwith a <a href="https://en.wikipedia.org/wiki/Sorting_algorithm#Stability">stable sorting algorithm</a>, so relative order between name-value pairs 12691cb0ef41Sopenharmony_ciwith the same name is preserved.</p> 12701cb0ef41Sopenharmony_ci<p>This method can be used, in particular, to increase cache hits.</p> 12711cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(<span class="hljs-string">'query[]=abc&type=search&query[]=123'</span>); 12721cb0ef41Sopenharmony_ciparams.<span class="hljs-title function_">sort</span>(); 12731cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>()); 12741cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints query%5B%5D=abc&query%5B%5D=123&type=search</span></code> <button class="copy-button">copy</button></pre> 12751cb0ef41Sopenharmony_ci<h5><code>urlSearchParams.toString()</code><span><a class="mark" href="#urlsearchparamstostring" id="urlsearchparamstostring">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_tostring"></a></h5> 12761cb0ef41Sopenharmony_ci<ul> 12771cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 12781cb0ef41Sopenharmony_ci</ul> 12791cb0ef41Sopenharmony_ci<p>Returns the search parameters serialized as a string, with characters 12801cb0ef41Sopenharmony_cipercent-encoded where necessary.</p> 12811cb0ef41Sopenharmony_ci<h5><code>urlSearchParams.values()</code><span><a class="mark" href="#urlsearchparamsvalues" id="urlsearchparamsvalues">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_values"></a></h5> 12821cb0ef41Sopenharmony_ci<ul> 12831cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 12841cb0ef41Sopenharmony_ci</ul> 12851cb0ef41Sopenharmony_ci<p>Returns an ES6 <code>Iterator</code> over the values of each name-value pair.</p> 12861cb0ef41Sopenharmony_ci<h5><code>urlSearchParams[Symbol.iterator]()</code><span><a class="mark" href="#urlsearchparamssymboliterator" id="urlsearchparamssymboliterator">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_symbol_iterator"></a></h5> 12871cb0ef41Sopenharmony_ci<ul> 12881cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 12891cb0ef41Sopenharmony_ci</ul> 12901cb0ef41Sopenharmony_ci<p>Returns an ES6 <code>Iterator</code> over each of the name-value pairs in the query string. 12911cb0ef41Sopenharmony_ciEach item of the iterator is a JavaScript <code>Array</code>. The first item of the <code>Array</code> 12921cb0ef41Sopenharmony_ciis the <code>name</code>, the second item of the <code>Array</code> is the <code>value</code>.</p> 12931cb0ef41Sopenharmony_ci<p>Alias for <a href="#urlsearchparamsentries"><code>urlSearchParams.entries()</code></a>.</p> 12941cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(<span class="hljs-string">'foo=bar&xyz=baz'</span>); 12951cb0ef41Sopenharmony_ci<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> [name, value] <span class="hljs-keyword">of</span> params) { 12961cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name, value); 12971cb0ef41Sopenharmony_ci} 12981cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 12991cb0ef41Sopenharmony_ci<span class="hljs-comment">// foo bar</span> 13001cb0ef41Sopenharmony_ci<span class="hljs-comment">// xyz baz</span></code> <button class="copy-button">copy</button></pre> 13011cb0ef41Sopenharmony_ci<h4><code>url.domainToASCII(domain)</code><span><a class="mark" href="#urldomaintoasciidomain" id="urldomaintoasciidomain">#</a></span><a aria-hidden="true" class="legacy" id="url_url_domaintoascii_domain"></a></h4> 13021cb0ef41Sopenharmony_ci<div class="api_metadata"> 13031cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 13041cb0ef41Sopenharmony_ci<table> 13051cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 13061cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td> 13071cb0ef41Sopenharmony_ci<td><p>ICU requirement is removed.</p></td></tr> 13081cb0ef41Sopenharmony_ci<tr><td>v7.4.0, v6.13.0</td> 13091cb0ef41Sopenharmony_ci<td><p><span>Added in: v7.4.0, v6.13.0</span></p></td></tr> 13101cb0ef41Sopenharmony_ci</tbody></table> 13111cb0ef41Sopenharmony_ci</details> 13121cb0ef41Sopenharmony_ci</div> 13131cb0ef41Sopenharmony_ci<ul> 13141cb0ef41Sopenharmony_ci<li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 13151cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 13161cb0ef41Sopenharmony_ci</ul> 13171cb0ef41Sopenharmony_ci<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 13181cb0ef41Sopenharmony_ciinvalid domain, the empty string is returned.</p> 13191cb0ef41Sopenharmony_ci<p>It performs the inverse operation to <a href="#urldomaintounicodedomain"><code>url.domainToUnicode()</code></a>.</p> 13201cb0ef41Sopenharmony_ci 13211cb0ef41Sopenharmony_ci<pre class="with-32-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> url <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>; 13221cb0ef41Sopenharmony_ci 13231cb0ef41Sopenharmony_ci<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>)); 13241cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints xn--espaol-zwa.com</span> 13251cb0ef41Sopenharmony_ci<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>)); 13261cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints xn--fiq228c.com</span> 13271cb0ef41Sopenharmony_ci<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>)); 13281cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints an empty string</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>); 13291cb0ef41Sopenharmony_ci 13301cb0ef41Sopenharmony_ci<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>)); 13311cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints xn--espaol-zwa.com</span> 13321cb0ef41Sopenharmony_ci<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>)); 13331cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints xn--fiq228c.com</span> 13341cb0ef41Sopenharmony_ci<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>)); 13351cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints an empty string</span></code><button class="copy-button">copy</button></pre> 13361cb0ef41Sopenharmony_ci<h4><code>url.domainToUnicode(domain)</code><span><a class="mark" href="#urldomaintounicodedomain" id="urldomaintounicodedomain">#</a></span><a aria-hidden="true" class="legacy" id="url_url_domaintounicode_domain"></a></h4> 13371cb0ef41Sopenharmony_ci<div class="api_metadata"> 13381cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 13391cb0ef41Sopenharmony_ci<table> 13401cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 13411cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td> 13421cb0ef41Sopenharmony_ci<td><p>ICU requirement is removed.</p></td></tr> 13431cb0ef41Sopenharmony_ci<tr><td>v7.4.0, v6.13.0</td> 13441cb0ef41Sopenharmony_ci<td><p><span>Added in: v7.4.0, v6.13.0</span></p></td></tr> 13451cb0ef41Sopenharmony_ci</tbody></table> 13461cb0ef41Sopenharmony_ci</details> 13471cb0ef41Sopenharmony_ci</div> 13481cb0ef41Sopenharmony_ci<ul> 13491cb0ef41Sopenharmony_ci<li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 13501cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 13511cb0ef41Sopenharmony_ci</ul> 13521cb0ef41Sopenharmony_ci<p>Returns the Unicode serialization of the <code>domain</code>. If <code>domain</code> is an invalid 13531cb0ef41Sopenharmony_cidomain, the empty string is returned.</p> 13541cb0ef41Sopenharmony_ci<p>It performs the inverse operation to <a href="#urldomaintoasciidomain"><code>url.domainToASCII()</code></a>.</p> 13551cb0ef41Sopenharmony_ci 13561cb0ef41Sopenharmony_ci<pre class="with-32-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> url <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>; 13571cb0ef41Sopenharmony_ci 13581cb0ef41Sopenharmony_ci<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>)); 13591cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints español.com</span> 13601cb0ef41Sopenharmony_ci<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>)); 13611cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 中文.com</span> 13621cb0ef41Sopenharmony_ci<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>)); 13631cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints an empty string</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>); 13641cb0ef41Sopenharmony_ci 13651cb0ef41Sopenharmony_ci<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>)); 13661cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints español.com</span> 13671cb0ef41Sopenharmony_ci<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>)); 13681cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 中文.com</span> 13691cb0ef41Sopenharmony_ci<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>)); 13701cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints an empty string</span></code><button class="copy-button">copy</button></pre> 13711cb0ef41Sopenharmony_ci<h4><code>url.fileURLToPath(url)</code><span><a class="mark" href="#urlfileurltopathurl" id="urlfileurltopathurl">#</a></span><a aria-hidden="true" class="legacy" id="url_url_fileurltopath_url"></a></h4> 13721cb0ef41Sopenharmony_ci<div class="api_metadata"> 13731cb0ef41Sopenharmony_ci<span>Added in: v10.12.0</span> 13741cb0ef41Sopenharmony_ci</div> 13751cb0ef41Sopenharmony_ci<ul> 13761cb0ef41Sopenharmony_ci<li><code>url</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The file URL string or URL object to convert to a path.</li> 13771cb0ef41Sopenharmony_ci<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> 13781cb0ef41Sopenharmony_ci</ul> 13791cb0ef41Sopenharmony_ci<p>This function ensures the correct decodings of percent-encoded characters as 13801cb0ef41Sopenharmony_ciwell as ensuring a cross-platform valid absolute path string.</p> 13811cb0ef41Sopenharmony_ci 13821cb0ef41Sopenharmony_ci<pre class="with-66-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { fileURLToPath } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>; 13831cb0ef41Sopenharmony_ci 13841cb0ef41Sopenharmony_ci<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>); 13851cb0ef41Sopenharmony_ci 13861cb0ef41Sopenharmony_ci<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> 13871cb0ef41Sopenharmony_ci<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///C:/path/'</span>); <span class="hljs-comment">// Correct: C:\path\ (Windows)</span> 13881cb0ef41Sopenharmony_ci 13891cb0ef41Sopenharmony_ci<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> 13901cb0ef41Sopenharmony_ci<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> 13911cb0ef41Sopenharmony_ci 13921cb0ef41Sopenharmony_ci<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> 13931cb0ef41Sopenharmony_ci<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///你好.txt'</span>); <span class="hljs-comment">// Correct: /你好.txt (POSIX)</span> 13941cb0ef41Sopenharmony_ci 13951cb0ef41Sopenharmony_ci<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> 13961cb0ef41Sopenharmony_ci<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///hello world'</span>); <span class="hljs-comment">// Correct: /hello world (POSIX)</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { fileURLToPath } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>); 13971cb0ef41Sopenharmony_ci<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> 13981cb0ef41Sopenharmony_ci<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///C:/path/'</span>); <span class="hljs-comment">// Correct: C:\path\ (Windows)</span> 13991cb0ef41Sopenharmony_ci 14001cb0ef41Sopenharmony_ci<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> 14011cb0ef41Sopenharmony_ci<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> 14021cb0ef41Sopenharmony_ci 14031cb0ef41Sopenharmony_ci<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> 14041cb0ef41Sopenharmony_ci<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///你好.txt'</span>); <span class="hljs-comment">// Correct: /你好.txt (POSIX)</span> 14051cb0ef41Sopenharmony_ci 14061cb0ef41Sopenharmony_ci<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> 14071cb0ef41Sopenharmony_ci<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///hello world'</span>); <span class="hljs-comment">// Correct: /hello world (POSIX)</span></code><button class="copy-button">copy</button></pre> 14081cb0ef41Sopenharmony_ci<h4><code>url.format(URL[, options])</code><span><a class="mark" href="#urlformaturl-options" id="urlformaturl-options">#</a></span><a aria-hidden="true" class="legacy" id="url_url_format_url_options"></a></h4> 14091cb0ef41Sopenharmony_ci<div class="api_metadata"> 14101cb0ef41Sopenharmony_ci<span>Added in: v7.6.0</span> 14111cb0ef41Sopenharmony_ci</div> 14121cb0ef41Sopenharmony_ci<ul> 14131cb0ef41Sopenharmony_ci<li><code>URL</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a> A <a href="#the-whatwg-url-api">WHATWG URL</a> object</li> 14141cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 14151cb0ef41Sopenharmony_ci<ul> 14161cb0ef41Sopenharmony_ci<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 14171cb0ef41Sopenharmony_ciusername and password, <code>false</code> otherwise. <strong>Default:</strong> <code>true</code>.</li> 14181cb0ef41Sopenharmony_ci<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 14191cb0ef41Sopenharmony_cifragment, <code>false</code> otherwise. <strong>Default:</strong> <code>true</code>.</li> 14201cb0ef41Sopenharmony_ci<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 14211cb0ef41Sopenharmony_cisearch query, <code>false</code> otherwise. <strong>Default:</strong> <code>true</code>.</li> 14221cb0ef41Sopenharmony_ci<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 14231cb0ef41Sopenharmony_cicomponent of the URL string should be encoded directly as opposed to being 14241cb0ef41Sopenharmony_ciPunycode encoded. <strong>Default:</strong> <code>false</code>.</li> 14251cb0ef41Sopenharmony_ci</ul> 14261cb0ef41Sopenharmony_ci</li> 14271cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 14281cb0ef41Sopenharmony_ci</ul> 14291cb0ef41Sopenharmony_ci<p>Returns a customizable serialization of a URL <code>String</code> representation of a 14301cb0ef41Sopenharmony_ci<a href="#the-whatwg-url-api">WHATWG URL</a> object.</p> 14311cb0ef41Sopenharmony_ci<p>The URL object has both a <code>toString()</code> method and <code>href</code> property that return 14321cb0ef41Sopenharmony_cistring serializations of the URL. These are not, however, customizable in 14331cb0ef41Sopenharmony_ciany way. The <code>url.format(URL[, options])</code> method allows for basic customization 14341cb0ef41Sopenharmony_ciof the output.</p> 14351cb0ef41Sopenharmony_ci 14361cb0ef41Sopenharmony_ci<pre class="with-48-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> url <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>; 14371cb0ef41Sopenharmony_ci<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>); 14381cb0ef41Sopenharmony_ci 14391cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 14401cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span> 14411cb0ef41Sopenharmony_ci 14421cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-title function_">toString</span>()); 14431cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span> 14441cb0ef41Sopenharmony_ci 14451cb0ef41Sopenharmony_ci<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> })); 14461cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 'https://測試/?abc'</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>); 14471cb0ef41Sopenharmony_ci<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>); 14481cb0ef41Sopenharmony_ci 14491cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 14501cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span> 14511cb0ef41Sopenharmony_ci 14521cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-title function_">toString</span>()); 14531cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span> 14541cb0ef41Sopenharmony_ci 14551cb0ef41Sopenharmony_ci<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> })); 14561cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 'https://測試/?abc'</span></code><button class="copy-button">copy</button></pre> 14571cb0ef41Sopenharmony_ci<h4><code>url.pathToFileURL(path)</code><span><a class="mark" href="#urlpathtofileurlpath" id="urlpathtofileurlpath">#</a></span><a aria-hidden="true" class="legacy" id="url_url_pathtofileurl_path"></a></h4> 14581cb0ef41Sopenharmony_ci<div class="api_metadata"> 14591cb0ef41Sopenharmony_ci<span>Added in: v10.12.0</span> 14601cb0ef41Sopenharmony_ci</div> 14611cb0ef41Sopenharmony_ci<ul> 14621cb0ef41Sopenharmony_ci<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> 14631cb0ef41Sopenharmony_ci<li>Returns: <a href="url.html#the-whatwg-url-api" class="type"><URL></a> The file URL object.</li> 14641cb0ef41Sopenharmony_ci</ul> 14651cb0ef41Sopenharmony_ci<p>This function ensures that <code>path</code> is resolved absolutely, and that the URL 14661cb0ef41Sopenharmony_cicontrol characters are correctly encoded when converting into a File URL.</p> 14671cb0ef41Sopenharmony_ci 14681cb0ef41Sopenharmony_ci<pre class="with-66-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { pathToFileURL } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>; 14691cb0ef41Sopenharmony_ci 14701cb0ef41Sopenharmony_ci<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> 14711cb0ef41Sopenharmony_ci<span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/foo#1'</span>); <span class="hljs-comment">// Correct: file:///foo%231 (POSIX)</span> 14721cb0ef41Sopenharmony_ci 14731cb0ef41Sopenharmony_ci<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> 14741cb0ef41Sopenharmony_ci<span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/some/path%.c'</span>); <span class="hljs-comment">// Correct: file:///some/path%25.c (POSIX)</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { pathToFileURL } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>); 14751cb0ef41Sopenharmony_ci<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(__filename); <span class="hljs-comment">// Incorrect: throws (POSIX)</span> 14761cb0ef41Sopenharmony_ci<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(__filename); <span class="hljs-comment">// Incorrect: C:\... (Windows)</span> 14771cb0ef41Sopenharmony_ci<span class="hljs-title function_">pathToFileURL</span>(__filename); <span class="hljs-comment">// Correct: file:///... (POSIX)</span> 14781cb0ef41Sopenharmony_ci<span class="hljs-title function_">pathToFileURL</span>(__filename); <span class="hljs-comment">// Correct: file:///C:/... (Windows)</span> 14791cb0ef41Sopenharmony_ci 14801cb0ef41Sopenharmony_ci<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> 14811cb0ef41Sopenharmony_ci<span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/foo#1'</span>); <span class="hljs-comment">// Correct: file:///foo%231 (POSIX)</span> 14821cb0ef41Sopenharmony_ci 14831cb0ef41Sopenharmony_ci<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> 14841cb0ef41Sopenharmony_ci<span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/some/path%.c'</span>); <span class="hljs-comment">// Correct: file:///some/path%25.c (POSIX)</span></code><button class="copy-button">copy</button></pre> 14851cb0ef41Sopenharmony_ci<h4><code>url.urlToHttpOptions(url)</code><span><a class="mark" href="#urlurltohttpoptionsurl" id="urlurltohttpoptionsurl">#</a></span><a aria-hidden="true" class="legacy" id="url_url_urltohttpoptions_url"></a></h4> 14861cb0ef41Sopenharmony_ci<div class="api_metadata"> 14871cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 14881cb0ef41Sopenharmony_ci<table> 14891cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 14901cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td> 14911cb0ef41Sopenharmony_ci<td><p>The returned object will also contain all the own enumerable properties of the <code>url</code> argument.</p></td></tr> 14921cb0ef41Sopenharmony_ci<tr><td>v15.7.0, v14.18.0</td> 14931cb0ef41Sopenharmony_ci<td><p><span>Added in: v15.7.0, v14.18.0</span></p></td></tr> 14941cb0ef41Sopenharmony_ci</tbody></table> 14951cb0ef41Sopenharmony_ci</details> 14961cb0ef41Sopenharmony_ci</div> 14971cb0ef41Sopenharmony_ci<ul> 14981cb0ef41Sopenharmony_ci<li><code>url</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a> The <a href="#the-whatwg-url-api">WHATWG URL</a> object to convert to an options object.</li> 14991cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Options object 15001cb0ef41Sopenharmony_ci<ul> 15011cb0ef41Sopenharmony_ci<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> 15021cb0ef41Sopenharmony_ci<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 15031cb0ef41Sopenharmony_cirequest to.</li> 15041cb0ef41Sopenharmony_ci<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> 15051cb0ef41Sopenharmony_ci<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> 15061cb0ef41Sopenharmony_ci<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> 15071cb0ef41Sopenharmony_ci<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. 15081cb0ef41Sopenharmony_ciE.G. <code>'/index.html?page=12'</code>. An exception is thrown when the request path 15091cb0ef41Sopenharmony_cicontains illegal characters. Currently, only spaces are rejected but that 15101cb0ef41Sopenharmony_cimay change in the future.</li> 15111cb0ef41Sopenharmony_ci<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> 15121cb0ef41Sopenharmony_ci<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> 15131cb0ef41Sopenharmony_ci<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 15141cb0ef41Sopenharmony_ciAuthorization header.</li> 15151cb0ef41Sopenharmony_ci</ul> 15161cb0ef41Sopenharmony_ci</li> 15171cb0ef41Sopenharmony_ci</ul> 15181cb0ef41Sopenharmony_ci<p>This utility function converts a URL object into an ordinary options object as 15191cb0ef41Sopenharmony_ciexpected by the <a href="http.html#httprequestoptions-callback"><code>http.request()</code></a> and <a href="https.html#httpsrequestoptions-callback"><code>https.request()</code></a> APIs.</p> 15201cb0ef41Sopenharmony_ci 15211cb0ef41Sopenharmony_ci<pre class="with-49-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { urlToHttpOptions } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>; 15221cb0ef41Sopenharmony_ci<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>); 15231cb0ef41Sopenharmony_ci 15241cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">urlToHttpOptions</span>(myURL)); 15251cb0ef41Sopenharmony_ci<span class="hljs-comment">/* 15261cb0ef41Sopenharmony_ci{ 15271cb0ef41Sopenharmony_ci protocol: 'https:', 15281cb0ef41Sopenharmony_ci hostname: 'xn--g6w251d', 15291cb0ef41Sopenharmony_ci hash: '#foo', 15301cb0ef41Sopenharmony_ci search: '?abc', 15311cb0ef41Sopenharmony_ci pathname: '/', 15321cb0ef41Sopenharmony_ci path: '/?abc', 15331cb0ef41Sopenharmony_ci href: 'https://a:b@xn--g6w251d/?abc#foo', 15341cb0ef41Sopenharmony_ci auth: 'a:b' 15351cb0ef41Sopenharmony_ci} 15361cb0ef41Sopenharmony_ci*/</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { urlToHttpOptions } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>); 15371cb0ef41Sopenharmony_ci<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>); 15381cb0ef41Sopenharmony_ci 15391cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">urlToHttpOptions</span>(myURL)); 15401cb0ef41Sopenharmony_ci<span class="hljs-comment">/* 15411cb0ef41Sopenharmony_ci{ 15421cb0ef41Sopenharmony_ci protocol: 'https:', 15431cb0ef41Sopenharmony_ci hostname: 'xn--g6w251d', 15441cb0ef41Sopenharmony_ci hash: '#foo', 15451cb0ef41Sopenharmony_ci search: '?abc', 15461cb0ef41Sopenharmony_ci pathname: '/', 15471cb0ef41Sopenharmony_ci path: '/?abc', 15481cb0ef41Sopenharmony_ci href: 'https://a:b@xn--g6w251d/?abc#foo', 15491cb0ef41Sopenharmony_ci auth: 'a:b' 15501cb0ef41Sopenharmony_ci} 15511cb0ef41Sopenharmony_ci*/</span></code><button class="copy-button">copy</button></pre> 15521cb0ef41Sopenharmony_ci</section><section><h3>Legacy URL API<span><a class="mark" href="#legacy-url-api" id="legacy-url-api">#</a></span><a aria-hidden="true" class="legacy" id="url_legacy_url_api"></a></h3> 15531cb0ef41Sopenharmony_ci<div class="api_metadata"> 15541cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 15551cb0ef41Sopenharmony_ci<table> 15561cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 15571cb0ef41Sopenharmony_ci<tr><td>v15.13.0, v14.17.0</td> 15581cb0ef41Sopenharmony_ci<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr> 15591cb0ef41Sopenharmony_ci<tr><td>v11.0.0</td> 15601cb0ef41Sopenharmony_ci<td><p>This API is deprecated.</p></td></tr> 15611cb0ef41Sopenharmony_ci</tbody></table> 15621cb0ef41Sopenharmony_ci</details> 15631cb0ef41Sopenharmony_ci</div> 15641cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use the WHATWG URL API instead.</div><p></p> 15651cb0ef41Sopenharmony_ci<h4>Legacy <code>urlObject</code><span><a class="mark" href="#legacy-urlobject" id="legacy-urlobject">#</a></span><a aria-hidden="true" class="legacy" id="url_legacy_urlobject"></a></h4> 15661cb0ef41Sopenharmony_ci<div class="api_metadata"> 15671cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 15681cb0ef41Sopenharmony_ci<table> 15691cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 15701cb0ef41Sopenharmony_ci<tr><td>v15.13.0, v14.17.0</td> 15711cb0ef41Sopenharmony_ci<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr> 15721cb0ef41Sopenharmony_ci<tr><td>v11.0.0</td> 15731cb0ef41Sopenharmony_ci<td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p></td></tr> 15741cb0ef41Sopenharmony_ci</tbody></table> 15751cb0ef41Sopenharmony_ci</details> 15761cb0ef41Sopenharmony_ci</div> 15771cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use the WHATWG URL API instead.</div><p></p> 15781cb0ef41Sopenharmony_ci<p>The legacy <code>urlObject</code> (<code>require('node:url').Url</code> or 15791cb0ef41Sopenharmony_ci<code>import { Url } from 'node:url'</code>) is 15801cb0ef41Sopenharmony_cicreated and returned by the <code>url.parse()</code> function.</p> 15811cb0ef41Sopenharmony_ci<h5><code>urlObject.auth</code><span><a class="mark" href="#urlobjectauth" id="urlobjectauth">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_auth"></a></h5> 15821cb0ef41Sopenharmony_ci<p>The <code>auth</code> property is the username and password portion of the URL, also 15831cb0ef41Sopenharmony_cireferred to as <em>userinfo</em>. This string subset follows the <code>protocol</code> and 15841cb0ef41Sopenharmony_cidouble slashes (if present) and precedes the <code>host</code> component, delimited by <code>@</code>. 15851cb0ef41Sopenharmony_ciThe string is either the username, or it is the username and password separated 15861cb0ef41Sopenharmony_ciby <code>:</code>.</p> 15871cb0ef41Sopenharmony_ci<p>For example: <code>'user:pass'</code>.</p> 15881cb0ef41Sopenharmony_ci<h5><code>urlObject.hash</code><span><a class="mark" href="#urlobjecthash" id="urlobjecthash">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_hash"></a></h5> 15891cb0ef41Sopenharmony_ci<p>The <code>hash</code> property is the fragment identifier portion of the URL including the 15901cb0ef41Sopenharmony_cileading <code>#</code> character.</p> 15911cb0ef41Sopenharmony_ci<p>For example: <code>'#hash'</code>.</p> 15921cb0ef41Sopenharmony_ci<h5><code>urlObject.host</code><span><a class="mark" href="#urlobjecthost" id="urlobjecthost">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_host"></a></h5> 15931cb0ef41Sopenharmony_ci<p>The <code>host</code> property is the full lower-cased host portion of the URL, including 15941cb0ef41Sopenharmony_cithe <code>port</code> if specified.</p> 15951cb0ef41Sopenharmony_ci<p>For example: <code>'sub.example.com:8080'</code>.</p> 15961cb0ef41Sopenharmony_ci<h5><code>urlObject.hostname</code><span><a class="mark" href="#urlobjecthostname" id="urlobjecthostname">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_hostname"></a></h5> 15971cb0ef41Sopenharmony_ci<p>The <code>hostname</code> property is the lower-cased host name portion of the <code>host</code> 15981cb0ef41Sopenharmony_cicomponent <em>without</em> the <code>port</code> included.</p> 15991cb0ef41Sopenharmony_ci<p>For example: <code>'sub.example.com'</code>.</p> 16001cb0ef41Sopenharmony_ci<h5><code>urlObject.href</code><span><a class="mark" href="#urlobjecthref" id="urlobjecthref">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_href"></a></h5> 16011cb0ef41Sopenharmony_ci<p>The <code>href</code> property is the full URL string that was parsed with both the 16021cb0ef41Sopenharmony_ci<code>protocol</code> and <code>host</code> components converted to lower-case.</p> 16031cb0ef41Sopenharmony_ci<p>For example: <code>'http://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'</code>.</p> 16041cb0ef41Sopenharmony_ci<h5><code>urlObject.path</code><span><a class="mark" href="#urlobjectpath" id="urlobjectpath">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_path"></a></h5> 16051cb0ef41Sopenharmony_ci<p>The <code>path</code> property is a concatenation of the <code>pathname</code> and <code>search</code> 16061cb0ef41Sopenharmony_cicomponents.</p> 16071cb0ef41Sopenharmony_ci<p>For example: <code>'/p/a/t/h?query=string'</code>.</p> 16081cb0ef41Sopenharmony_ci<p>No decoding of the <code>path</code> is performed.</p> 16091cb0ef41Sopenharmony_ci<h5><code>urlObject.pathname</code><span><a class="mark" href="#urlobjectpathname" id="urlobjectpathname">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_pathname"></a></h5> 16101cb0ef41Sopenharmony_ci<p>The <code>pathname</code> property consists of the entire path section of the URL. This 16111cb0ef41Sopenharmony_ciis everything following the <code>host</code> (including the <code>port</code>) and before the start 16121cb0ef41Sopenharmony_ciof the <code>query</code> or <code>hash</code> components, delimited by either the ASCII question 16131cb0ef41Sopenharmony_cimark (<code>?</code>) or hash (<code>#</code>) characters.</p> 16141cb0ef41Sopenharmony_ci<p>For example: <code>'/p/a/t/h'</code>.</p> 16151cb0ef41Sopenharmony_ci<p>No decoding of the path string is performed.</p> 16161cb0ef41Sopenharmony_ci<h5><code>urlObject.port</code><span><a class="mark" href="#urlobjectport" id="urlobjectport">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_port"></a></h5> 16171cb0ef41Sopenharmony_ci<p>The <code>port</code> property is the numeric port portion of the <code>host</code> component.</p> 16181cb0ef41Sopenharmony_ci<p>For example: <code>'8080'</code>.</p> 16191cb0ef41Sopenharmony_ci<h5><code>urlObject.protocol</code><span><a class="mark" href="#urlobjectprotocol" id="urlobjectprotocol">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_protocol"></a></h5> 16201cb0ef41Sopenharmony_ci<p>The <code>protocol</code> property identifies the URL's lower-cased protocol scheme.</p> 16211cb0ef41Sopenharmony_ci<p>For example: <code>'http:'</code>.</p> 16221cb0ef41Sopenharmony_ci<h5><code>urlObject.query</code><span><a class="mark" href="#urlobjectquery" id="urlobjectquery">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_query"></a></h5> 16231cb0ef41Sopenharmony_ci<p>The <code>query</code> property is either the query string without the leading ASCII 16241cb0ef41Sopenharmony_ciquestion mark (<code>?</code>), or an object returned by the <a href="querystring.html"><code>querystring</code></a> module's 16251cb0ef41Sopenharmony_ci<code>parse()</code> method. Whether the <code>query</code> property is a string or object is 16261cb0ef41Sopenharmony_cidetermined by the <code>parseQueryString</code> argument passed to <code>url.parse()</code>.</p> 16271cb0ef41Sopenharmony_ci<p>For example: <code>'query=string'</code> or <code>{'query': 'string'}</code>.</p> 16281cb0ef41Sopenharmony_ci<p>If returned as a string, no decoding of the query string is performed. If 16291cb0ef41Sopenharmony_cireturned as an object, both keys and values are decoded.</p> 16301cb0ef41Sopenharmony_ci<h5><code>urlObject.search</code><span><a class="mark" href="#urlobjectsearch" id="urlobjectsearch">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_search"></a></h5> 16311cb0ef41Sopenharmony_ci<p>The <code>search</code> property consists of the entire "query string" portion of the 16321cb0ef41Sopenharmony_ciURL, including the leading ASCII question mark (<code>?</code>) character.</p> 16331cb0ef41Sopenharmony_ci<p>For example: <code>'?query=string'</code>.</p> 16341cb0ef41Sopenharmony_ci<p>No decoding of the query string is performed.</p> 16351cb0ef41Sopenharmony_ci<h5><code>urlObject.slashes</code><span><a class="mark" href="#urlobjectslashes" id="urlobjectslashes">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_slashes"></a></h5> 16361cb0ef41Sopenharmony_ci<p>The <code>slashes</code> property is a <code>boolean</code> with a value of <code>true</code> if two ASCII 16371cb0ef41Sopenharmony_ciforward-slash characters (<code>/</code>) are required following the colon in the 16381cb0ef41Sopenharmony_ci<code>protocol</code>.</p> 16391cb0ef41Sopenharmony_ci<h4><code>url.format(urlObject)</code><span><a class="mark" href="#urlformaturlobject" id="urlformaturlobject">#</a></span><a aria-hidden="true" class="legacy" id="url_url_format_urlobject"></a></h4> 16401cb0ef41Sopenharmony_ci<div class="api_metadata"> 16411cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 16421cb0ef41Sopenharmony_ci<table> 16431cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 16441cb0ef41Sopenharmony_ci<tr><td>v17.0.0</td> 16451cb0ef41Sopenharmony_ci<td><p>Now throws an <code>ERR_INVALID_URL</code> exception when Punycode conversion of a hostname introduces changes that could cause the URL to be re-parsed differently.</p></td></tr> 16461cb0ef41Sopenharmony_ci<tr><td>v15.13.0, v14.17.0</td> 16471cb0ef41Sopenharmony_ci<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr> 16481cb0ef41Sopenharmony_ci<tr><td>v11.0.0</td> 16491cb0ef41Sopenharmony_ci<td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p></td></tr> 16501cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td> 16511cb0ef41Sopenharmony_ci<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> 16521cb0ef41Sopenharmony_ci<tr><td>v0.1.25</td> 16531cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.25</span></p></td></tr> 16541cb0ef41Sopenharmony_ci</tbody></table> 16551cb0ef41Sopenharmony_ci</details> 16561cb0ef41Sopenharmony_ci</div> 16571cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use the WHATWG URL API instead.</div><p></p> 16581cb0ef41Sopenharmony_ci<ul> 16591cb0ef41Sopenharmony_ci<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 16601cb0ef41Sopenharmony_ciconstructed otherwise). If a string, it is converted to an object by passing 16611cb0ef41Sopenharmony_ciit to <code>url.parse()</code>.</li> 16621cb0ef41Sopenharmony_ci</ul> 16631cb0ef41Sopenharmony_ci<p>The <code>url.format()</code> method returns a formatted URL string derived from 16641cb0ef41Sopenharmony_ci<code>urlObject</code>.</p> 16651cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>); 16661cb0ef41Sopenharmony_ciurl.<span class="hljs-title function_">format</span>({ 16671cb0ef41Sopenharmony_ci <span class="hljs-attr">protocol</span>: <span class="hljs-string">'https'</span>, 16681cb0ef41Sopenharmony_ci <span class="hljs-attr">hostname</span>: <span class="hljs-string">'example.com'</span>, 16691cb0ef41Sopenharmony_ci <span class="hljs-attr">pathname</span>: <span class="hljs-string">'/some/path'</span>, 16701cb0ef41Sopenharmony_ci <span class="hljs-attr">query</span>: { 16711cb0ef41Sopenharmony_ci <span class="hljs-attr">page</span>: <span class="hljs-number">1</span>, 16721cb0ef41Sopenharmony_ci <span class="hljs-attr">format</span>: <span class="hljs-string">'json'</span>, 16731cb0ef41Sopenharmony_ci }, 16741cb0ef41Sopenharmony_ci}); 16751cb0ef41Sopenharmony_ci 16761cb0ef41Sopenharmony_ci<span class="hljs-comment">// => 'https://example.com/some/path?page=1&format=json'</span></code> <button class="copy-button">copy</button></pre> 16771cb0ef41Sopenharmony_ci<p>If <code>urlObject</code> is not an object or a string, <code>url.format()</code> will throw a 16781cb0ef41Sopenharmony_ci<a href="errors.html#class-typeerror"><code>TypeError</code></a>.</p> 16791cb0ef41Sopenharmony_ci<p>The formatting process operates as follows:</p> 16801cb0ef41Sopenharmony_ci<ul> 16811cb0ef41Sopenharmony_ci<li>A new empty string <code>result</code> is created.</li> 16821cb0ef41Sopenharmony_ci<li>If <code>urlObject.protocol</code> is a string, it is appended as-is to <code>result</code>.</li> 16831cb0ef41Sopenharmony_ci<li>Otherwise, if <code>urlObject.protocol</code> is not <code>undefined</code> and is not a string, an 16841cb0ef41Sopenharmony_ci<a href="errors.html#class-error"><code>Error</code></a> is thrown.</li> 16851cb0ef41Sopenharmony_ci<li>For all string values of <code>urlObject.protocol</code> that <em>do not end</em> with an ASCII 16861cb0ef41Sopenharmony_cicolon (<code>:</code>) character, the literal string <code>:</code> will be appended to <code>result</code>.</li> 16871cb0ef41Sopenharmony_ci<li>If either of the following conditions is true, then the literal string <code>//</code> 16881cb0ef41Sopenharmony_ciwill be appended to <code>result</code>: 16891cb0ef41Sopenharmony_ci<ul> 16901cb0ef41Sopenharmony_ci<li><code>urlObject.slashes</code> property is true;</li> 16911cb0ef41Sopenharmony_ci<li><code>urlObject.protocol</code> begins with <code>http</code>, <code>https</code>, <code>ftp</code>, <code>gopher</code>, or 16921cb0ef41Sopenharmony_ci<code>file</code>;</li> 16931cb0ef41Sopenharmony_ci</ul> 16941cb0ef41Sopenharmony_ci</li> 16951cb0ef41Sopenharmony_ci<li>If the value of the <code>urlObject.auth</code> property is truthy, and either 16961cb0ef41Sopenharmony_ci<code>urlObject.host</code> or <code>urlObject.hostname</code> are not <code>undefined</code>, the value of 16971cb0ef41Sopenharmony_ci<code>urlObject.auth</code> will be coerced into a string and appended to <code>result</code> 16981cb0ef41Sopenharmony_cifollowed by the literal string <code>@</code>.</li> 16991cb0ef41Sopenharmony_ci<li>If the <code>urlObject.host</code> property is <code>undefined</code> then: 17001cb0ef41Sopenharmony_ci<ul> 17011cb0ef41Sopenharmony_ci<li>If the <code>urlObject.hostname</code> is a string, it is appended to <code>result</code>.</li> 17021cb0ef41Sopenharmony_ci<li>Otherwise, if <code>urlObject.hostname</code> is not <code>undefined</code> and is not a string, 17031cb0ef41Sopenharmony_cian <a href="errors.html#class-error"><code>Error</code></a> is thrown.</li> 17041cb0ef41Sopenharmony_ci<li>If the <code>urlObject.port</code> property value is truthy, and <code>urlObject.hostname</code> 17051cb0ef41Sopenharmony_ciis not <code>undefined</code>: 17061cb0ef41Sopenharmony_ci<ul> 17071cb0ef41Sopenharmony_ci<li>The literal string <code>:</code> is appended to <code>result</code>, and</li> 17081cb0ef41Sopenharmony_ci<li>The value of <code>urlObject.port</code> is coerced to a string and appended to 17091cb0ef41Sopenharmony_ci<code>result</code>.</li> 17101cb0ef41Sopenharmony_ci</ul> 17111cb0ef41Sopenharmony_ci</li> 17121cb0ef41Sopenharmony_ci</ul> 17131cb0ef41Sopenharmony_ci</li> 17141cb0ef41Sopenharmony_ci<li>Otherwise, if the <code>urlObject.host</code> property value is truthy, the value of 17151cb0ef41Sopenharmony_ci<code>urlObject.host</code> is coerced to a string and appended to <code>result</code>.</li> 17161cb0ef41Sopenharmony_ci<li>If the <code>urlObject.pathname</code> property is a string that is not an empty string: 17171cb0ef41Sopenharmony_ci<ul> 17181cb0ef41Sopenharmony_ci<li>If the <code>urlObject.pathname</code> <em>does not start</em> with an ASCII forward slash 17191cb0ef41Sopenharmony_ci(<code>/</code>), then the literal string <code>'/'</code> is appended to <code>result</code>.</li> 17201cb0ef41Sopenharmony_ci<li>The value of <code>urlObject.pathname</code> is appended to <code>result</code>.</li> 17211cb0ef41Sopenharmony_ci</ul> 17221cb0ef41Sopenharmony_ci</li> 17231cb0ef41Sopenharmony_ci<li>Otherwise, if <code>urlObject.pathname</code> is not <code>undefined</code> and is not a string, an 17241cb0ef41Sopenharmony_ci<a href="errors.html#class-error"><code>Error</code></a> is thrown.</li> 17251cb0ef41Sopenharmony_ci<li>If the <code>urlObject.search</code> property is <code>undefined</code> and if the <code>urlObject.query</code> 17261cb0ef41Sopenharmony_ciproperty is an <code>Object</code>, the literal string <code>?</code> is appended to <code>result</code> 17271cb0ef41Sopenharmony_cifollowed by the output of calling the <a href="querystring.html"><code>querystring</code></a> module's <code>stringify()</code> 17281cb0ef41Sopenharmony_cimethod passing the value of <code>urlObject.query</code>.</li> 17291cb0ef41Sopenharmony_ci<li>Otherwise, if <code>urlObject.search</code> is a string: 17301cb0ef41Sopenharmony_ci<ul> 17311cb0ef41Sopenharmony_ci<li>If the value of <code>urlObject.search</code> <em>does not start</em> with the ASCII question 17321cb0ef41Sopenharmony_cimark (<code>?</code>) character, the literal string <code>?</code> is appended to <code>result</code>.</li> 17331cb0ef41Sopenharmony_ci<li>The value of <code>urlObject.search</code> is appended to <code>result</code>.</li> 17341cb0ef41Sopenharmony_ci</ul> 17351cb0ef41Sopenharmony_ci</li> 17361cb0ef41Sopenharmony_ci<li>Otherwise, if <code>urlObject.search</code> is not <code>undefined</code> and is not a string, an 17371cb0ef41Sopenharmony_ci<a href="errors.html#class-error"><code>Error</code></a> is thrown.</li> 17381cb0ef41Sopenharmony_ci<li>If the <code>urlObject.hash</code> property is a string: 17391cb0ef41Sopenharmony_ci<ul> 17401cb0ef41Sopenharmony_ci<li>If the value of <code>urlObject.hash</code> <em>does not start</em> with the ASCII hash (<code>#</code>) 17411cb0ef41Sopenharmony_cicharacter, the literal string <code>#</code> is appended to <code>result</code>.</li> 17421cb0ef41Sopenharmony_ci<li>The value of <code>urlObject.hash</code> is appended to <code>result</code>.</li> 17431cb0ef41Sopenharmony_ci</ul> 17441cb0ef41Sopenharmony_ci</li> 17451cb0ef41Sopenharmony_ci<li>Otherwise, if the <code>urlObject.hash</code> property is not <code>undefined</code> and is not a 17461cb0ef41Sopenharmony_cistring, an <a href="errors.html#class-error"><code>Error</code></a> is thrown.</li> 17471cb0ef41Sopenharmony_ci<li><code>result</code> is returned.</li> 17481cb0ef41Sopenharmony_ci</ul> 17491cb0ef41Sopenharmony_ci<h4><code>url.parse(urlString[, parseQueryString[, slashesDenoteHost]])</code><span><a class="mark" href="#urlparseurlstring-parsequerystring-slashesdenotehost" id="urlparseurlstring-parsequerystring-slashesdenotehost">#</a></span><a aria-hidden="true" class="legacy" id="url_url_parse_urlstring_parsequerystring_slashesdenotehost"></a></h4> 17501cb0ef41Sopenharmony_ci<div class="api_metadata"> 17511cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 17521cb0ef41Sopenharmony_ci<table> 17531cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 17541cb0ef41Sopenharmony_ci<tr><td>v18.13.0</td> 17551cb0ef41Sopenharmony_ci<td><p>Documentation-only deprecation.</p></td></tr> 17561cb0ef41Sopenharmony_ci<tr><td>v15.13.0, v14.17.0</td> 17571cb0ef41Sopenharmony_ci<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr> 17581cb0ef41Sopenharmony_ci<tr><td>v11.14.0</td> 17591cb0ef41Sopenharmony_ci<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> 17601cb0ef41Sopenharmony_ci<tr><td>v11.0.0</td> 17611cb0ef41Sopenharmony_ci<td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p></td></tr> 17621cb0ef41Sopenharmony_ci<tr><td>v9.0.0</td> 17631cb0ef41Sopenharmony_ci<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> 17641cb0ef41Sopenharmony_ci<tr><td>v0.1.25</td> 17651cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.25</span></p></td></tr> 17661cb0ef41Sopenharmony_ci</tbody></table> 17671cb0ef41Sopenharmony_ci</details> 17681cb0ef41Sopenharmony_ci</div> 17691cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use the WHATWG URL API instead.</div><p></p> 17701cb0ef41Sopenharmony_ci<ul> 17711cb0ef41Sopenharmony_ci<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> 17721cb0ef41Sopenharmony_ci<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 17731cb0ef41Sopenharmony_cibe set to an object returned by the <a href="querystring.html"><code>querystring</code></a> module's <code>parse()</code> 17741cb0ef41Sopenharmony_cimethod. If <code>false</code>, the <code>query</code> property on the returned URL object will be an 17751cb0ef41Sopenharmony_ciunparsed, undecoded string. <strong>Default:</strong> <code>false</code>.</li> 17761cb0ef41Sopenharmony_ci<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 17771cb0ef41Sopenharmony_cistring <code>//</code> and preceding the next <code>/</code> will be interpreted as the <code>host</code>. 17781cb0ef41Sopenharmony_ciFor instance, given <code>//foo/bar</code>, the result would be 17791cb0ef41Sopenharmony_ci<code>{host: 'foo', pathname: '/bar'}</code> rather than <code>{pathname: '//foo/bar'}</code>. 17801cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 17811cb0ef41Sopenharmony_ci</ul> 17821cb0ef41Sopenharmony_ci<p>The <code>url.parse()</code> method takes a URL string, parses it, and returns a URL 17831cb0ef41Sopenharmony_ciobject.</p> 17841cb0ef41Sopenharmony_ci<p>A <code>TypeError</code> is thrown if <code>urlString</code> is not a string.</p> 17851cb0ef41Sopenharmony_ci<p>A <code>URIError</code> is thrown if the <code>auth</code> property is present but cannot be decoded.</p> 17861cb0ef41Sopenharmony_ci<p><code>url.parse()</code> uses a lenient, non-standard algorithm for parsing URL 17871cb0ef41Sopenharmony_cistrings. It is prone to security issues such as <a href="https://hackerone.com/reports/678487">host name spoofing</a> 17881cb0ef41Sopenharmony_ciand incorrect handling of usernames and passwords. Do not use with untrusted 17891cb0ef41Sopenharmony_ciinput. CVEs are not issued for <code>url.parse()</code> vulnerabilities. Use the 17901cb0ef41Sopenharmony_ci<a href="#the-whatwg-url-api">WHATWG URL</a> API instead.</p> 17911cb0ef41Sopenharmony_ci<h4><code>url.resolve(from, to)</code><span><a class="mark" href="#urlresolvefrom-to" id="urlresolvefrom-to">#</a></span><a aria-hidden="true" class="legacy" id="url_url_resolve_from_to"></a></h4> 17921cb0ef41Sopenharmony_ci<div class="api_metadata"> 17931cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 17941cb0ef41Sopenharmony_ci<table> 17951cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 17961cb0ef41Sopenharmony_ci<tr><td>v15.13.0, v14.17.0</td> 17971cb0ef41Sopenharmony_ci<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr> 17981cb0ef41Sopenharmony_ci<tr><td>v11.0.0</td> 17991cb0ef41Sopenharmony_ci<td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p></td></tr> 18001cb0ef41Sopenharmony_ci<tr><td>v6.6.0</td> 18011cb0ef41Sopenharmony_ci<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> 18021cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 18031cb0ef41Sopenharmony_ci<td><p>The <code>auth</code> fields is cleared now the <code>to</code> parameter contains a hostname.</p></td></tr> 18041cb0ef41Sopenharmony_ci<tr><td>v6.5.0, v4.6.2</td> 18051cb0ef41Sopenharmony_ci<td><p>The <code>port</code> field is copied correctly now.</p></td></tr> 18061cb0ef41Sopenharmony_ci<tr><td>v0.1.25</td> 18071cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.25</span></p></td></tr> 18081cb0ef41Sopenharmony_ci</tbody></table> 18091cb0ef41Sopenharmony_ci</details> 18101cb0ef41Sopenharmony_ci</div> 18111cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use the WHATWG URL API instead.</div><p></p> 18121cb0ef41Sopenharmony_ci<ul> 18131cb0ef41Sopenharmony_ci<li><code>from</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The base URL to use if <code>to</code> is a relative URL.</li> 18141cb0ef41Sopenharmony_ci<li><code>to</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The target URL to resolve.</li> 18151cb0ef41Sopenharmony_ci</ul> 18161cb0ef41Sopenharmony_ci<p>The <code>url.resolve()</code> method resolves a target URL relative to a base URL in a 18171cb0ef41Sopenharmony_cimanner similar to that of a web browser resolving an anchor tag.</p> 18181cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>); 18191cb0ef41Sopenharmony_ciurl.<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> 18201cb0ef41Sopenharmony_ciurl.<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> 18211cb0ef41Sopenharmony_ciurl.<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'http://example.com/one'</span>, <span class="hljs-string">'/two'</span>); <span class="hljs-comment">// 'http://example.com/two'</span></code> <button class="copy-button">copy</button></pre> 18221cb0ef41Sopenharmony_ci<p>To achieve the same result using the WHATWG URL API:</p> 18231cb0ef41Sopenharmony_ci<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>) { 18241cb0ef41Sopenharmony_ci <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>)); 18251cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (resolvedUrl.<span class="hljs-property">protocol</span> === <span class="hljs-string">'resolve:'</span>) { 18261cb0ef41Sopenharmony_ci <span class="hljs-comment">// `from` is a relative URL.</span> 18271cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> { pathname, search, hash } = resolvedUrl; 18281cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> pathname + search + hash; 18291cb0ef41Sopenharmony_ci } 18301cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> resolvedUrl.<span class="hljs-title function_">toString</span>(); 18311cb0ef41Sopenharmony_ci} 18321cb0ef41Sopenharmony_ci 18331cb0ef41Sopenharmony_ci<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> 18341cb0ef41Sopenharmony_ci<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> 18351cb0ef41Sopenharmony_ci<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'http://example.com/one'</span>, <span class="hljs-string">'/two'</span>); <span class="hljs-comment">// 'http://example.com/two'</span></code> <button class="copy-button">copy</button></pre> 18361cb0ef41Sopenharmony_ci<p><a id="whatwg-percent-encoding"></a></p> 18371cb0ef41Sopenharmony_ci</section><section><h3>Percent-encoding in URLs<span><a class="mark" href="#percent-encoding-in-urls" id="percent-encoding-in-urls">#</a></span><a aria-hidden="true" class="legacy" id="url_percent_encoding_in_urls"></a></h3> 18381cb0ef41Sopenharmony_ci<p>URLs are permitted to only contain a certain range of characters. Any character 18391cb0ef41Sopenharmony_cifalling outside of that range must be encoded. How such characters are encoded, 18401cb0ef41Sopenharmony_ciand which characters to encode depends entirely on where the character is 18411cb0ef41Sopenharmony_cilocated within the structure of the URL.</p> 18421cb0ef41Sopenharmony_ci<h4>Legacy API<span><a class="mark" href="#legacy-api" id="legacy-api">#</a></span><a aria-hidden="true" class="legacy" id="url_legacy_api"></a></h4> 18431cb0ef41Sopenharmony_ci<p>Within the Legacy API, spaces (<code>' '</code>) and the following characters will be 18441cb0ef41Sopenharmony_ciautomatically escaped in the properties of URL objects:</p> 18451cb0ef41Sopenharmony_ci<pre><code class="language-text">< > " ` \r \n \t { } | \ ^ '</code> <button class="copy-button">copy</button></pre> 18461cb0ef41Sopenharmony_ci<p>For example, the ASCII space character (<code>' '</code>) is encoded as <code>%20</code>. The ASCII 18471cb0ef41Sopenharmony_ciforward slash (<code>/</code>) character is encoded as <code>%3C</code>.</p> 18481cb0ef41Sopenharmony_ci<h4>WHATWG API<span><a class="mark" href="#whatwg-api" id="whatwg-api">#</a></span><a aria-hidden="true" class="legacy" id="url_whatwg_api"></a></h4> 18491cb0ef41Sopenharmony_ci<p>The <a href="https://url.spec.whatwg.org/">WHATWG URL Standard</a> uses a more selective and fine grained approach to 18501cb0ef41Sopenharmony_ciselecting encoded characters than that used by the Legacy API.</p> 18511cb0ef41Sopenharmony_ci<p>The WHATWG algorithm defines four "percent-encode sets" that describe ranges 18521cb0ef41Sopenharmony_ciof characters that must be percent-encoded:</p> 18531cb0ef41Sopenharmony_ci<ul> 18541cb0ef41Sopenharmony_ci<li> 18551cb0ef41Sopenharmony_ci<p>The <em>C0 control percent-encode set</em> includes code points in range U+0000 to 18561cb0ef41Sopenharmony_ciU+001F (inclusive) and all code points greater than U+007E (~).</p> 18571cb0ef41Sopenharmony_ci</li> 18581cb0ef41Sopenharmony_ci<li> 18591cb0ef41Sopenharmony_ci<p>The <em>fragment percent-encode set</em> includes the <em>C0 control percent-encode set</em> 18601cb0ef41Sopenharmony_ciand code points U+0020 SPACE, U+0022 ("), U+003C (<), U+003E (>), 18611cb0ef41Sopenharmony_ciand U+0060 (`).</p> 18621cb0ef41Sopenharmony_ci</li> 18631cb0ef41Sopenharmony_ci<li> 18641cb0ef41Sopenharmony_ci<p>The <em>path percent-encode set</em> includes the <em>C0 control percent-encode set</em> 18651cb0ef41Sopenharmony_ciand code points U+0020 SPACE, U+0022 ("), U+0023 (#), U+003C (<), U+003E (>), 18661cb0ef41Sopenharmony_ciU+003F (?), U+0060 (`), U+007B ({), and U+007D (}).</p> 18671cb0ef41Sopenharmony_ci</li> 18681cb0ef41Sopenharmony_ci<li> 18691cb0ef41Sopenharmony_ci<p>The <em>userinfo encode set</em> includes the <em>path percent-encode set</em> and code 18701cb0ef41Sopenharmony_cipoints U+002F (/), U+003A (:), U+003B (;), U+003D (=), U+0040 (@), 18711cb0ef41Sopenharmony_ciU+005B ([) to U+005E(^), and U+007C (|).</p> 18721cb0ef41Sopenharmony_ci</li> 18731cb0ef41Sopenharmony_ci</ul> 18741cb0ef41Sopenharmony_ci<p>The <em>userinfo percent-encode set</em> is used exclusively for username and 18751cb0ef41Sopenharmony_cipasswords encoded within the URL. The <em>path percent-encode set</em> is used for the 18761cb0ef41Sopenharmony_cipath of most URLs. The <em>fragment percent-encode set</em> is used for URL fragments. 18771cb0ef41Sopenharmony_ciThe <em>C0 control percent-encode set</em> is used for host and path under certain 18781cb0ef41Sopenharmony_cispecific conditions, in addition to all other cases.</p> 18791cb0ef41Sopenharmony_ci<p>When non-ASCII characters appear within a host name, the host name is encoded 18801cb0ef41Sopenharmony_ciusing 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 18811cb0ef41Sopenharmony_ci<em>both</em> Punycode encoded and percent-encoded characters:</p> 18821cb0ef41Sopenharmony_ci<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>); 18831cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>); 18841cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://xn--1xa.example.com/foo</span> 18851cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">origin</span>); 18861cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints https://xn--1xa.example.com</span></code> <button class="copy-button">copy</button></pre></section> 18871cb0ef41Sopenharmony_ci <!-- API END --> 18881cb0ef41Sopenharmony_ci </div> 18891cb0ef41Sopenharmony_ci </div> 18901cb0ef41Sopenharmony_ci </div> 18911cb0ef41Sopenharmony_ci</body> 18921cb0ef41Sopenharmony_ci</html> 1893