Gets the hash portion of the URL
Gets the hash portion of the URL
the hash value to set
Gets the host portion of the URL
Gets the host portion of the URL
the host value to set
Gets the hostname portion of the URL
Gets the hostname portion of the URL
the hostname value to set
Gets the href, which is equivalent to calling toString()
Gets the href portion of the URL
the href value to set
Gets the origin portion of the URL
Gets the password portion of the URL
Gets the password portion of the URL
the password value to set
Gets the pathname portion of the URL
Gets the pathname portion of the URL
the pathname value to set
Gets the port portion of the URL. Default ports are automatically transformed to an empty string
Gets the port portion of the URL
the port value to set
Gets the protocol portion of the URL
Gets the protocol portion of the URL
the protocol value to set
Gets the search portion of the URL
Gets the search portion of the URL
the search value to set
Gets the search parameters portion of the URL. Cannot be set, use the search
property
Gets the username portion of the URL
Gets the username portion of the URL
the username value to set
Iterates of the characters in the string
Converts the object into a primitive
The string to use for the default toString
function
Converts the primitive into it's JSON representation
the object to serialize with JSON.stringify
Converts the primitive into it's string representation. Often has a unit value associated with it such as 125ms
Retrieves the actual value of the number
Implements a primitive that represents a URL value. It is a string primitive so can be passed to any API that requires a string or string primitive. It also provides the WHATWG properties.
The
Url
primitive needs a globalURL
implementation of the WHATWG URL specification. This can easily be added on modern Node versions:import { URL } from 'url'; global.URL = URL
The UMD module automatically performs this installation.
However, if native Node modules cannot be used (React Native) the
url-polyfill
package can be used to install a pure JavaScript version of the API.const url = new Url('https://abc:xyz@example.com:899/some/path?param=one#hash'); console.log(url.origin); // 'https://example.com'; console.log(url.host); // 'example.com:899'; console.log(url.hostname); // 'example.com'; console.log(url.username); // 'abc'; console.log(url.password); // 'xyz'; console.log(url.search); // '?param=one'; console.log(url.searchParams.get('param')); // 'one'; console.log(url.protocol); // 'https:'; console.log(url.port); // '899'; console.log(url.href); // 'https://abc:xyz@example.com:899/some/path?param=one#hash'; console.log(url.toString()); // 'https://abc:xyz@example.com:899/some/path?param=one#hash'; console.log(url[Symbol.toPrimitive]('string')); // 'https://abc:xyz@example.com:899/some/path?param=one#hash';