UNPKG

learnyounode

Version:

Learn You The Node.js For Much Win! An intro to Node.js via a set of self-guided workshops.

639 lines (575 loc) 124 kB
<!DOCTYPE html><html lang="en"><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>URL | Node.js v12.10.0 Documentation</title> <link rel="stylesheet" href="data:text/css;base64,LyogbGF0aW4tZXh0ICovCkBmb250LWZhY2UgewogIGZvbnQtZmFtaWx5OiAnTGF0byc7CiAgZm9udC1zdHlsZTogaXRhbGljOwogIGZvbnQtd2VpZ2h0OiA0MDA7CiAgc3JjOiBsb2NhbCgnTGF0byBJdGFsaWMnKSwgbG9jYWwoJ0xhdG8tSXRhbGljJyksIHVybChodHRwczovL2ZvbnRzLmdzdGF0aWMuY29tL3MvbGF0by92MTYvUzZ1OHc0Qk1VVFBIanhzQVVpLXFOaVhnN2VVMC53b2ZmMikgZm9ybWF0KCd3b2ZmMicpOwogIHVuaWNvZGUtcmFuZ2U6IFUrMDEwMC0wMjRGLCBVKzAyNTksIFUrMUUwMC0xRUZGLCBVKzIwMjAsIFUrMjBBMC0yMEFCLCBVKzIwQUQtMjBDRiwgVSsyMTEzLCBVKzJDNjAtMkM3RiwgVStBNzIwLUE3RkY7Cn0KLyogbGF0aW4gKi8KQGZvbnQtZmFjZSB7CiAgZm9udC1mYW1pbHk6ICdMYXRvJzsKICBmb250LXN0eWxlOiBpdGFsaWM7CiAgZm9udC13ZWlnaHQ6IDQwMDsKICBzcmM6IGxvY2FsKCdMYXRvIEl0YWxpYycpLCBsb2NhbCgnTGF0by1JdGFsaWMnKSwgdXJsKGh0dHBzOi8vZm9udHMuZ3N0YXRpYy5jb20vcy9sYXRvL3YxNi9TNnU4dzRCTVVUUEhqeHNBWEMtcU5pWGc3US53b2ZmMikgZm9ybWF0KCd3b2ZmMicpOwogIHVuaWNvZGUtcmFuZ2U6IFUrMDAwMC0wMEZGLCBVKzAxMzEsIFUrMDE1Mi0wMTUzLCBVKzAyQkItMDJCQywgVSswMkM2LCBVKzAyRE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tZW50IHsKICBjb2xvcjogIzY2NjsKICBmb250LXdlaWdodDogbGlnaHRlcjsKfQo="> <link rel="canonical" href="https://nodejs.org/api/url.html"> </head> <body class="alt apidoc" id="api-section-url"> <div id="content" class="clearfix"> <div id="column2" class="interior"> <div id="intro" class="interior"> <a href="https://nodejs.org/" title="Go back to the home page"> Node.js </a> </div> <ul> <li><a href="https://nodejs.org/api/documentation.html" class="nav-documentation">About these Docs</a></li> <li><a href="https://nodejs.org/api/synopsis.html" class="nav-synopsis">Usage &amp; Example</a></li> </ul> <div class="line"></div> <ul> <li><a href="https://nodejs.org/api/assert.html" class="nav-assert">Assertion Testing</a></li> <li><a href="https://nodejs.org/api/async_hooks.html" class="nav-async_hooks">Async Hooks</a></li> <li><a href="https://nodejs.org/api/buffer.html" class="nav-buffer">Buffer</a></li> <li><a href="https://nodejs.org/api/addons.html" class="nav-addons">C++ Addons</a></li> <li><a href="https://nodejs.org/api/n-api.html" class="nav-n-api">C/C++ Addons - N-API</a></li> <li><a href="https://nodejs.org/api/child_process.html" class="nav-child_process">Child Processes</a></li> <li><a href="https://nodejs.org/api/cluster.html" class="nav-cluster">Cluster</a></li> <li><a href="https://nodejs.org/api/cli.html" class="nav-cli">Command Line Options</a></li> <li><a href="https://nodejs.org/api/console.html" class="nav-console">Console</a></li> <li><a href="https://nodejs.org/api/crypto.html" class="nav-crypto">Crypto</a></li> <li><a href="https://nodejs.org/api/debugger.html" class="nav-debugger">Debugger</a></li> <li><a href="https://nodejs.org/api/deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> <li><a href="https://nodejs.org/api/dns.html" class="nav-dns">DNS</a></li> <li><a href="https://nodejs.org/api/domain.html" class="nav-domain">Domain</a></li> <li><a href="https://nodejs.org/api/esm.html" class="nav-esm">ECMAScript Modules</a></li> <li><a href="https://nodejs.org/api/errors.html" class="nav-errors">Errors</a></li> <li><a href="https://nodejs.org/api/events.html" class="nav-events">Events</a></li> <li><a href="https://nodejs.org/api/fs.html" class="nav-fs">File System</a></li> <li><a href="https://nodejs.org/api/globals.html" class="nav-globals">Globals</a></li> <li><a href="https://nodejs.org/api/http.html" class="nav-http">HTTP</a></li> <li><a href="https://nodejs.org/api/http2.html" class="nav-http2">HTTP/2</a></li> <li><a href="https://nodejs.org/api/https.html" class="nav-https">HTTPS</a></li> <li><a href="https://nodejs.org/api/inspector.html" class="nav-inspector">Inspector</a></li> <li><a href="https://nodejs.org/api/intl.html" class="nav-intl">Internationalization</a></li> <li><a href="https://nodejs.org/api/modules.html" class="nav-modules">Modules</a></li> <li><a href="https://nodejs.org/api/net.html" class="nav-net">Net</a></li> <li><a href="https://nodejs.org/api/os.html" class="nav-os">OS</a></li> <li><a href="https://nodejs.org/api/path.html" class="nav-path">Path</a></li> <li><a href="https://nodejs.org/api/perf_hooks.html" class="nav-perf_hooks">Performance Hooks</a></li> <li><a href="https://nodejs.org/api/policy.html" class="nav-policy">Policies</a></li> <li><a href="https://nodejs.org/api/process.html" class="nav-process">Process</a></li> <li><a href="https://nodejs.org/api/punycode.html" class="nav-punycode">Punycode</a></li> <li><a href="https://nodejs.org/api/querystring.html" class="nav-querystring">Query Strings</a></li> <li><a href="https://nodejs.org/api/readline.html" class="nav-readline">Readline</a></li> <li><a href="https://nodejs.org/api/repl.html" class="nav-repl">REPL</a></li> <li><a href="https://nodejs.org/api/report.html" class="nav-report">Report</a></li> <li><a href="https://nodejs.org/api/stream.html" class="nav-stream">Stream</a></li> <li><a href="https://nodejs.org/api/string_decoder.html" class="nav-string_decoder">String Decoder</a></li> <li><a href="https://nodejs.org/api/timers.html" class="nav-timers">Timers</a></li> <li><a href="https://nodejs.org/api/tls.html" class="nav-tls">TLS/SSL</a></li> <li><a href="https://nodejs.org/api/tracing.html" class="nav-tracing">Trace Events</a></li> <li><a href="https://nodejs.org/api/tty.html" class="nav-tty">TTY</a></li> <li><a href="https://nodejs.org/api/dgram.html" class="nav-dgram">UDP/Datagram</a></li> <li><a href="https://nodejs.org/api/url.html" class="nav-url active">URL</a></li> <li><a href="https://nodejs.org/api/util.html" class="nav-util">Utilities</a></li> <li><a href="https://nodejs.org/api/v8.html" class="nav-v8">V8</a></li> <li><a href="https://nodejs.org/api/vm.html" class="nav-vm">VM</a></li> <li><a href="https://nodejs.org/api/worker_threads.html" class="nav-worker_threads">Worker Threads</a></li> <li><a href="https://nodejs.org/api/zlib.html" class="nav-zlib">Zlib</a></li> </ul> <div class="line"></div> <ul> <li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">GitHub Repo &amp; Issue Tracker</a></li> </ul> </div> <div id="column1" data-id="url" class="interior"> <header> <h1>Node.js v12.10.0 Documentation</h1> <div id="gtoc"> <ul> <li> <a href="https://nodejs.org/api/index.html" name="toc">Index</a> </li> <li> <a href="https://nodejs.org/api/all.html">View on single page</a> </li> <li> <a href="https://nodejs.org/api/url.json">View as JSON</a> </li> <li class="version-picker"> <a href="#">View another version <span>▼</span></a> <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v12.x/api/url.html">12.x</a></li> <li><a href="https://nodejs.org/docs/latest-v11.x/api/url.html">11.x</a></li> <li><a href="https://nodejs.org/docs/latest-v10.x/api/url.html">10.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v9.x/api/url.html">9.x</a></li> <li><a href="https://nodejs.org/docs/latest-v8.x/api/url.html">8.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v7.x/api/url.html">7.x</a></li> <li><a href="https://nodejs.org/docs/latest-v6.x/api/url.html">6.x</a></li> <li><a href="https://nodejs.org/docs/latest-v5.x/api/url.html">5.x</a></li> <li><a href="https://nodejs.org/docs/latest-v4.x/api/url.html">4.x</a></li> <li><a href="https://nodejs.org/docs/latest-v0.12.x/api/url.html">0.12.x</a></li> <li><a href="https://nodejs.org/docs/latest-v0.10.x/api/url.html">0.10.x</a></li></ol> </li> <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/url.md"><span class="github_icon"><svg height="16" width="16" viewBox="0 0 16.1 16.1" fill="currentColor"><path d="M8 0a8 8 0 0 0-2.5 15.6c.4 0 .5-.2.5-.4v-1.5c-2 .4-2.5-.5-2.7-1 0-.1-.5-.9-.8-1-.3-.2-.7-.6 0-.6.6 0 1 .6 1.2.8.7 1.2 1.9 1 2.4.7 0-.5.2-.9.5-1-1.8-.3-3.7-1-3.7-4 0-.9.3-1.6.8-2.2 0-.2-.3-1 .1-2 0 0 .7-.3 2.2.7a7.4 7.4 0 0 1 4 0c1.5-1 2.2-.8 2.2-.8.5 1.1.2 2 .1 2.1.5.6.8 1.3.8 2.2 0 3-1.9 3.7-3.6 4 .3.2.5.7.5 1.4v2.2c0 .2.1.5.5.4A8 8 0 0 0 16 8a8 8 0 0 0-8-8z"></path></svg></span>Edit on GitHub</a></li> </ul> </div> <hr> </header> <div id="toc"> <h2>Table of Contents</h2> <ul> <li> <p><span class="stability_2"><a href="#url_url">URL</a></span></p> <ul> <li><a href="#url_url_strings_and_url_objects">URL Strings and URL Objects</a></li> <li> <p><a href="#url_the_whatwg_url_api">The WHATWG URL API</a></p> <ul> <li> <p><a href="#url_class_url">Class: URL</a></p> <ul> <li><a href="#url_constructor_new_url_input_base">Constructor: new URL(input[, base])</a></li> <li><a href="#url_url_hash">url.hash</a></li> <li><a href="#url_url_host">url.host</a></li> <li><a href="#url_url_hostname">url.hostname</a></li> <li><a href="#url_url_href">url.href</a></li> <li><a href="#url_url_origin">url.origin</a></li> <li><a href="#url_url_password">url.password</a></li> <li><a href="#url_url_pathname">url.pathname</a></li> <li><a href="#url_url_port">url.port</a></li> <li> <p><a href="#url_url_protocol">url.protocol</a></p> <ul> <li><a href="#url_special_schemes">Special Schemes</a></li> </ul> </li> <li><a href="#url_url_search">url.search</a></li> <li><a href="#url_url_searchparams">url.searchParams</a></li> <li><a href="#url_url_username">url.username</a></li> <li><a href="#url_url_tostring">url.toString()</a></li> <li><a href="#url_url_tojson">url.toJSON()</a></li> </ul> </li> <li> <p><a href="#url_class_urlsearchparams">Class: URLSearchParams</a></p> <ul> <li><a href="#url_constructor_new_urlsearchparams">Constructor: new URLSearchParams()</a></li> <li><a href="#url_constructor_new_urlsearchparams_string">Constructor: new URLSearchParams(string)</a></li> <li><a href="#url_constructor_new_urlsearchparams_obj">Constructor: new URLSearchParams(obj)</a></li> <li><a href="#url_constructor_new_urlsearchparams_iterable">Constructor: new URLSearchParams(iterable)</a></li> <li><a href="#url_urlsearchparams_append_name_value">urlSearchParams.append(name, value)</a></li> <li><a href="#url_urlsearchparams_delete_name">urlSearchParams.delete(name)</a></li> <li><a href="#url_urlsearchparams_entries">urlSearchParams.entries()</a></li> <li><a href="#url_urlsearchparams_foreach_fn_thisarg">urlSearchParams.forEach(fn[, thisArg])</a></li> <li><a href="#url_urlsearchparams_get_name">urlSearchParams.get(name)</a></li> <li><a href="#url_urlsearchparams_getall_name">urlSearchParams.getAll(name)</a></li> <li><a href="#url_urlsearchparams_has_name">urlSearchParams.has(name)</a></li> <li><a href="#url_urlsearchparams_keys">urlSearchParams.keys()</a></li> <li><a href="#url_urlsearchparams_set_name_value">urlSearchParams.set(name, value)</a></li> <li><a href="#url_urlsearchparams_sort">urlSearchParams.sort()</a></li> <li><a href="#url_urlsearchparams_tostring">urlSearchParams.toString()</a></li> <li><a href="#url_urlsearchparams_values">urlSearchParams.values()</a></li> <li><a href="#url_urlsearchparams_symbol_iterator">urlSearchParams[Symbol.iterator]()</a></li> </ul> </li> <li><a href="#url_url_domaintoascii_domain">url.domainToASCII(domain)</a></li> <li><a href="#url_url_domaintounicode_domain">url.domainToUnicode(domain)</a></li> <li><a href="#url_url_fileurltopath_url">url.fileURLToPath(url)</a></li> <li><a href="#url_url_format_url_options">url.format(URL[, options])</a></li> <li><a href="#url_url_pathtofileurl_path">url.pathToFileURL(path)</a></li> </ul> </li> <li> <p><a href="#url_legacy_url_api">Legacy URL API</a></p> <ul> <li> <p><a href="#url_legacy_urlobject">Legacy <code>urlObject</code></a></p> <ul> <li><a href="#url_urlobject_auth">urlObject.auth</a></li> <li><a href="#url_urlobject_hash">urlObject.hash</a></li> <li><a href="#url_urlobject_host">urlObject.host</a></li> <li><a href="#url_urlobject_hostname">urlObject.hostname</a></li> <li><a href="#url_urlobject_href">urlObject.href</a></li> <li><a href="#url_urlobject_path">urlObject.path</a></li> <li><a href="#url_urlobject_pathname">urlObject.pathname</a></li> <li><a href="#url_urlobject_port">urlObject.port</a></li> <li><a href="#url_urlobject_protocol">urlObject.protocol</a></li> <li><a href="#url_urlobject_query">urlObject.query</a></li> <li><a href="#url_urlobject_search">urlObject.search</a></li> <li><a href="#url_urlobject_slashes">urlObject.slashes</a></li> </ul> </li> <li><a href="#url_url_format_urlobject">url.format(urlObject)</a></li> <li><a href="#url_url_parse_urlstring_parsequerystring_slashesdenotehost">url.parse(urlString[, parseQueryString[, slashesDenoteHost]])</a></li> <li><a href="#url_url_resolve_from_to">url.resolve(from, to)</a></li> </ul> </li> <li> <p><a href="#url_percent_encoding_in_urls">Percent-Encoding in URLs</a></p> <ul> <li><a href="#url_legacy_api">Legacy API</a></li> <li><a href="#url_whatwg_api">WHATWG API</a></li> </ul> </li> </ul> </li> </ul> </div> <div id="apicontent"> <h1>URL<span><a class="mark" href="#url_url" id="url_url">#</a></span></h1> <p></p><div class="api_stability api_stability_2"><a href="https://nodejs.org/api/documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p> <p>The <code>url</code> module provides utilities for URL resolution and parsing. It can be accessed using:</p> <pre><code class="language-js">const url = require('url'); </code></pre> <h2>URL Strings and URL Objects<span><a class="mark" href="#url_url_strings_and_url_objects" id="url_url_strings_and_url_objects">#</a></span></h2> <p>A URL string is a structured string containing multiple meaningful components. When parsed, a URL object is returned containing properties for each of these components.</p> <p>The <code>url</code> module provides two APIs for working with URLs: a legacy API that is Node.js specific, and a newer API that implements the same <a href="https://url.spec.whatwg.org/">WHATWG URL Standard</a> used by web browsers.</p> <p>While the Legacy API has not been deprecated, it is maintained solely for backwards compatibility with existing applications. New application code should use the WHATWG API.</p> <p>A comparison between the WHATWG and Legacy APIs is provided below. Above the URL <code>'http://user:<a href="https://nodejs.org/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cfbfaebcbc8fbcbaade1aab7aea2bfa3aae1aca0a2">[email&nbsp;protected]</a>:8080/p/a/t/h?query=string#hash'</code>, properties of an object returned by the legacy <code>url.parse()</code> are shown. Below it are properties of a WHATWG <code>URL</code> object.</p> <p>WHATWG URL's <code>origin</code> property includes <code>protocol</code> and <code>host</code>, but not <code>username</code> or <code>password</code>.</p> <pre><code class="language-txt">┌────────────────────────────────────────────────────────────────────────────────────────────────┐ │ href │ ├──────────┬──┬─────────────────────┬────────────────────────┬───────────────────────────┬───────┤ │ protocol │ │ auth │ host │ path │ hash │ │ │ │ ├─────────────────┬──────┼──────────┬────────────────┤ │ │ │ │ │ hostname │ port │ pathname │ search │ │ │ │ │ │ │ │ ├─┬──────────────┤ │ │ │ │ │ │ │ │ │ query │ │ " https: // user : pass @ sub.example.com : 8080 /p/a/t/h ? query=string #hash " │ │ │ │ │ hostname │ port │ │ │ │ │ │ │ │ ├─────────────────┴──────┤ │ │ │ │ protocol │ │ username │ password │ host │ │ │ │ ├──────────┴──┼──────────┴──────────┼────────────────────────┤ │ │ │ │ origin │ │ origin │ pathname │ search │ hash │ ├─────────────┴─────────────────────┴────────────────────────┴──────────┴────────────────┴───────┤ │ href │ └────────────────────────────────────────────────────────────────────────────────────────────────┘ (all spaces in the "" line should be ignored — they are purely for formatting) </code></pre> <p>Parsing the URL string using the WHATWG API:</p> <pre><code class="language-js">const myURL = new URL('https://user:<a href="https://nodejs.org/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b6c6d7c5c5f6c5c3d498d3ced7dbc6dad398d5d9db">[email&nbsp;protected]</a>:8080/p/a/t/h?query=string#hash'); </code></pre> <p>Parsing the URL string using the Legacy API:</p> <pre><code class="language-js">const url = require('url'); const myURL = url.parse('https://user:<a href="https://nodejs.org/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="77071604043704021559120f161a071b125914181a">[email&nbsp;protected]</a>:8080/p/a/t/h?query=string#hash'); </code></pre> <h2>The WHATWG URL API<span><a class="mark" href="#url_the_whatwg_url_api" id="url_the_whatwg_url_api">#</a></span></h2> <h3>Class: URL<span><a class="mark" href="#url_class_url" id="url_class_url">#</a></span></h3> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The class is now available on the global object.</p></td></tr> <tr><td>v7.0.0, v6.13.0</td> <td><p><span>Added in: v7.0.0, v6.13.0</span></p></td></tr> </tbody></table> </details> </div> <p>Browser-compatible <code>URL</code> class, implemented by following the WHATWG URL Standard. <a href="https://url.spec.whatwg.org/#example-url-parsing">Examples of parsed URLs</a> may be found in the Standard itself. The <code>URL</code> class is also available on the global object.</p> <p>In accordance with browser conventions, all properties of <code>URL</code> objects are implemented as getters and setters on the class prototype, rather than as data properties on the object itself. Thus, unlike <a href="#url_legacy_urlobject">legacy <code>urlObject</code></a>s, using 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 return <code>true</code>.</p> <h4>Constructor: new URL(input[, base])<span><a class="mark" href="#url_constructor_new_url_input_base" id="url_constructor_new_url_input_base">#</a></span></h4> <ul> <li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> The absolute or relative input URL to parse. If <code>input</code> is relative, then <code>base</code> is required. If <code>input</code> is absolute, the <code>base</code> is ignored.</li> <li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | <a href="https://nodejs.org/api/url.html#url_the_whatwg_url_api" class="type">&lt;URL&gt;</a> The base URL to resolve against if the <code>input</code> is not absolute.</li> </ul> <p>Creates a new <code>URL</code> object by parsing the <code>input</code> relative to the <code>base</code>. If <code>base</code> is passed as a string, it will be parsed equivalent to <code>new URL(base)</code>.</p> <pre><code class="language-js">const myURL = new URL('/foo', 'https://example.org/'); // https://example.org/foo </code></pre> <p>A <code>TypeError</code> will be thrown if the <code>input</code> or <code>base</code> are not valid URLs. Note that an effort will be made to coerce the given values into strings. For instance:</p> <pre><code class="language-js">const myURL = new URL({ toString: () =&gt; 'https://example.org/' }); // https://example.org/ </code></pre> <p>Unicode characters appearing within the hostname of <code>input</code> will be automatically converted to ASCII using the <a href="https://tools.ietf.org/html/rfc5891#section-4.4">Punycode</a> algorithm.</p> <pre><code class="language-js">const myURL = new URL('https://測試'); // https://xn--g6w251d/ </code></pre> <p>This feature is only available if the <code>node</code> executable was compiled with <a href="https://nodejs.org/api/intl.html#intl_options_for_building_node_js">ICU</a> enabled. If not, the domain names are passed through unchanged.</p> <p>In cases where it is not known in advance if <code>input</code> is an absolute URL and a <code>base</code> is provided, it is advised to validate that the <code>origin</code> of the <code>URL</code> object is what is expected.</p> <pre><code class="language-js">let myURL = new URL('http://Example.com/', 'https://example.org/'); // http://example.com/ myURL = new URL('https://Example.com/', 'https://example.org/'); // https://example.com/ myURL = new URL('foo://Example.com/', 'https://example.org/'); // foo://Example.com/ myURL = new URL('http:Example.com/', 'https://example.org/'); // http://example.com/ myURL = new URL('https:Example.com/', 'https://example.org/'); // https://example.org/Example.com/ myURL = new URL('foo:Example.com/', 'https://example.org/'); // foo:Example.com/ </code></pre> <h4>url.hash<span><a class="mark" href="#url_url_hash" id="url_url_hash">#</a></span></h4> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li> </ul> <p>Gets and sets the fragment portion of the URL.</p> <pre><code class="language-js">const myURL = new URL('https://example.org/foo#bar'); console.log(myURL.hash); // Prints #bar myURL.hash = 'baz'; console.log(myURL.href); // Prints https://example.org/foo#baz </code></pre> <p>Invalid URL characters included in the value assigned to the <code>hash</code> property are <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which characters to percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_parsequerystring_slashesdenotehost"><code>url.parse()</code></a> and <a href="#url_url_format_urlobject"><code>url.format()</code></a> methods would produce.</p> <h4>url.host<span><a class="mark" href="#url_url_host" id="url_url_host">#</a></span></h4> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li> </ul> <p>Gets and sets the host portion of the URL.</p> <pre><code class="language-js">const myURL = new URL('https://example.org:81/foo'); console.log(myURL.host); // Prints example.org:81 myURL.host = 'example.com:82'; console.log(myURL.href); // Prints https://example.com:82/foo </code></pre> <p>Invalid host values assigned to the <code>host</code> property are ignored.</p> <h4>url.hostname<span><a class="mark" href="#url_url_hostname" id="url_url_hostname">#</a></span></h4> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li> </ul> <p>Gets and sets the hostname portion of the URL. The key difference between <code>url.host</code> and <code>url.hostname</code> is that <code>url.hostname</code> does <em>not</em> include the port.</p> <pre><code class="language-js">const myURL = new URL('https://example.org:81/foo'); console.log(myURL.hostname); // Prints example.org myURL.hostname = 'example.com:82'; console.log(myURL.href); // Prints https://example.com:81/foo </code></pre> <p>Invalid hostname values assigned to the <code>hostname</code> property are ignored.</p> <h4>url.href<span><a class="mark" href="#url_url_href" id="url_url_href">#</a></span></h4> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li> </ul> <p>Gets and sets the serialized URL.</p> <pre><code class="language-js">const myURL = new URL('https://example.org/foo'); console.log(myURL.href); // Prints https://example.org/foo myURL.href = 'https://example.com/bar'; console.log(myURL.href); // Prints https://example.com/bar </code></pre> <p>Getting the value of the <code>href</code> property is equivalent to calling <a href="#url_url_tostring"><code>url.toString()</code></a>.</p> <p>Setting the value of this property to a new value is equivalent to creating a new <code>URL</code> object using <a href="#url_constructor_new_url_input_base"><code>new URL(value)</code></a>. Each of the <code>URL</code> object's properties will be modified.</p> <p>If the value assigned to the <code>href</code> property is not a valid URL, a <code>TypeError</code> will be thrown.</p> <h4>url.origin<span><a class="mark" href="#url_url_origin" id="url_url_origin">#</a></span></h4> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li> </ul> <p>Gets the read-only serialization of the URL's origin.</p> <pre><code class="language-js">const myURL = new URL('https://example.org/foo/bar?baz'); console.log(myURL.origin); // Prints https://example.org </code></pre> <pre><code class="language-js">const idnURL = new URL('https://測試'); console.log(idnURL.origin); // Prints https://xn--g6w251d console.log(idnURL.hostname); // Prints xn--g6w251d </code></pre> <h4>url.password<span><a class="mark" href="#url_url_password" id="url_url_password">#</a></span></h4> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li> </ul> <p>Gets and sets the password portion of the URL.</p> <pre><code class="language-js">const myURL = new URL('https://abc:<a href="https://nodejs.org/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6c1415162c09140d011c0009420f0301">[email&nbsp;protected]</a>'); console.log(myURL.password); // Prints xyz myURL.password = '123'; console.log(myURL.href); // Prints https://abc:<a href="https://nodejs.org/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1d2c2f2e5d78657c706d7178337e7270">[email&nbsp;protected]</a> </code></pre> <p>Invalid URL characters included in the value assigned to the <code>password</code> property are <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which characters to percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_parsequerystring_slashesdenotehost"><code>url.parse()</code></a> and <a href="#url_url_format_urlobject"><code>url.format()</code></a> methods would produce.</p> <h4>url.pathname<span><a class="mark" href="#url_url_pathname" id="url_url_pathname">#</a></span></h4> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li> </ul> <p>Gets and sets the path portion of the URL.</p> <pre><code class="language-js">const myURL = new URL('https://example.org/abc/xyz?123'); console.log(myURL.pathname); // Prints /abc/xyz myURL.pathname = '/abcdef'; console.log(myURL.href); // Prints https://example.org/abcdef?123 </code></pre> <p>Invalid URL characters included in the value assigned to the <code>pathname</code> property are <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which characters to percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_parsequerystring_slashesdenotehost"><code>url.parse()</code></a> and <a href="#url_url_format_urlobject"><code>url.format()</code></a> methods would produce.</p> <h4>url.port<span><a class="mark" href="#url_url_port" id="url_url_port">#</a></span></h4> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li> </ul> <p>Gets and sets the port portion of the URL.</p> <p>The port value may be a number or a string containing a number in the range <code>0</code> to <code>65535</code> (inclusive). Setting the value to the default port of the <code>URL</code> objects given <code>protocol</code> will result in the <code>port</code> value becoming the empty string (<code>''</code>).</p> <p>The port value can be an empty string in which case the port depends on the protocol/scheme:</p> <table><thead><tr><th align="left">protocol</th><th align="left">port</th></tr></thead><tbody><tr><td align="left">"ftp"</td><td align="left">21</td></tr><tr><td align="left">"file"</td><td align="left"></td></tr><tr><td align="left">"gopher"</td><td align="left">70</td></tr><tr><td align="left">"http"</td><td align="left">80</td></tr><tr><td align="left">"https"</td><td align="left">443</td></tr><tr><td align="left">"ws"</td><td align="left">80</td></tr><tr><td align="left">"wss"</td><td align="left">443</td></tr></tbody></table> <p>Upon assigning a value to the port, the value will first be converted to a string using <code>.toString()</code>.</p> <p>If that string is invalid but it begins with a number, the leading number is assigned to <code>port</code>. If the number lies outside the range denoted above, it is ignored.</p> <pre><code class="language-js">const myURL = new URL('https://example.org:8888'); console.log(myURL.port); // Prints 8888 // Default ports are automatically transformed to the empty string // (HTTPS protocol's default port is 443) myURL.port = '443'; console.log(myURL.port); // Prints the empty string console.log(myURL.href); // Prints https://example.org/ myURL.port = 1234; console.log(myURL.port); // Prints 1234 console.log(myURL.href); // Prints https://example.org:1234/ // Completely invalid port strings are ignored myURL.port = 'abcd'; console.log(myURL.port); // Prints 1234 // Leading numbers are treated as a port number myURL.port = '5678abcd'; console.log(myURL.port); // Prints 5678 // Non-integers are truncated myURL.port = 1234.5678; console.log(myURL.port); // Prints 1234 // Out-of-range numbers which are not represented in scientific notation // will be ignored. myURL.port = 1e10; // 10000000000, will be range-checked as described below console.log(myURL.port); // Prints 1234 </code></pre> <p>Numbers which contain a decimal point, such as floating-point numbers or numbers in scientific notation, are not an exception to this rule. Leading numbers up to the decimal point will be set as the URL's port, assuming they are valid:</p> <pre><code class="language-js">myURL.port = 4.567e21; console.log(myURL.port); // Prints 4 (because it is the leading number in the string '4.567e21') </code></pre> <h4>url.protocol<span><a class="mark" href="#url_url_protocol" id="url_url_protocol">#</a></span></h4> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li> </ul> <p>Gets and sets the protocol portion of the URL.</p> <pre><code class="language-js">const myURL = new URL('https://example.org'); console.log(myURL.protocol); // Prints https: myURL.protocol = 'ftp'; console.log(myURL.href); // Prints ftp://example.org/ </code></pre> <p>Invalid URL protocol values assigned to the <code>protocol</code> property are ignored.</p> <h5>Special Schemes<span><a class="mark" href="#url_special_schemes" id="url_special_schemes">#</a></span></h5> <p>The <a href="https://url.spec.whatwg.org/">WHATWG URL Standard</a> considers a handful of URL protocol schemes to be <em>special</em> in terms of how they are parsed and serialized. When a URL is parsed using one of these special protocols, the <code>url.protocol</code> property may be changed to another special protocol but cannot be changed to a non-special protocol, and vice versa.</p> <p>For instance, changing from <code>http</code> to <code>https</code> works:</p> <pre><code class="language-js">const u = new URL('http://example.org'); u.protocol = 'https'; console.log(u.href); // https://example.org </code></pre> <p>However, changing from <code>http</code> to a hypothetical <code>fish</code> protocol does not because the new protocol is not special.</p> <pre><code class="language-js">const u = new URL('http://example.org'); u.protocol = 'fish'; console.log(u.href); // http://example.org </code></pre> <p>Likewise, changing from a non-special protocol to a special protocol is also not permitted:</p> <pre><code class="language-js">const u = new URL('fish://example.org'); u.protocol = 'http'; console.log(u.href); // fish://example.org </code></pre> <p>According to the WHATWG URL Standard, special protocol schemes are <code>ftp</code>, <code>file</code>, <code>gopher</code>, <code>http</code>, <code>https</code>, <code>ws</code>, and <code>wss</code>.</p> <h4>url.search<span><a class="mark" href="#url_url_search" id="url_url_search">#</a></span></h4> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li> </ul> <p>Gets and sets the serialized query portion of the URL.</p> <pre><code class="language-js">const myURL = new URL('https://example.org/abc?123'); console.log(myURL.search); // Prints ?123 myURL.search = 'abc=xyz'; console.log(myURL.href); // Prints https://example.org/abc?abc=xyz </code></pre> <p>Any invalid URL characters appearing in the value assigned the <code>search</code> property will be <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which characters to percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_parsequerystring_slashesdenotehost"><code>url.parse()</code></a> and <a href="#url_url_format_urlobject"><code>url.format()</code></a> methods would produce.</p> <h4>url.searchParams<span><a class="mark" href="#url_url_searchparams" id="url_url_searchparams">#</a></span></h4> <ul> <li><a href="https://nodejs.org/api/url.html#url_class_urlsearchparams" class="type">&lt;URLSearchParams&gt;</a></li> </ul> <p>Gets the <a href="#url_class_urlsearchparams"><code>URLSearchParams</code></a> object representing the query parameters of the URL. This property is read-only; to replace the entirety of query parameters of the URL, use the <a href="#url_url_search"><code>url.search</code></a> setter. See <a href="#url_class_urlsearchparams"><code>URLSearchParams</code></a> documentation for details.</p> <h4>url.username<span><a class="mark" href="#url_url_username" id="url_url_username">#</a></span></h4> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li> </ul> <p>Gets and sets the username portion of the URL.</p> <pre><code class="language-js">const myURL = new URL('https://abc:<a href="https://nodejs.org/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3b4342417b5e435a564b575e15585456">[email&nbsp;protected]</a>'); console.log(myURL.username); // Prints abc myURL.username = '123'; console.log(myURL.href); // Prints https://123:<a href="https://nodejs.org/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dba3a2a19bbea3bab6abb7bef5b8b4b6">[email&nbsp;protected]</a>/ </code></pre> <p>Any invalid URL characters appearing in the value assigned the <code>username</code> property will be <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which characters to percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_parsequerystring_slashesdenotehost"><code>url.parse()</code></a> and <a href="#url_url_format_urlo