URL

interface URL : Serializable, ProxyObject

URL

The URL class is universally supported across JavaScript engines and browser implementations, and behaves similarly in each case; it is used for parsing well-formed URLs and extracting their constituent parts.

 

Summary

"The URL interface is used to parse, construct, normalize, and encode URLs. It works by providing properties which allow you to easily read and modify the components of a URL. You normally create a new URL object by specifying the URL as a string when calling its constructor, or by providing a relative URL and a base URL. You can then easily read the parsed components of the URL or make changes to the URL. If a browser doesn't yet support the URL() constructor, you can access a URL object using the Window interface's URL property. Be sure to check to see if any of your target browsers require this to be prefixed."

 

Specification compliance

The URL constructor and object layout are defined as part of the WhatWG URL Specification. Elide's implementation of URL is based on Java's standard java.net.URI class and is (experimentally) compliant with the WhatWG URL Specification.

Special behavior

URLs are usable in various other places in the Web APIs, and also have a custom stringification function to make them easy to use when printing or logging. Simply calling .toString() on a URL object produces a string with the absolute URL.

 

Further reading

See also:

Support for other standards

URL objects are a common primitive used by other, higher-order JavaScript ecosystem standards. Most notably, URL objects can be used with the Fetch API. URLs can be passed to the constructor for Request.

Inheritors

Types

Link copied to clipboard
interface URLConstructors

This interface defines the surface area of expected constructors for standard URL objects. According to spec, URL objects must be constructable from a plain string or from another URL. Empty URLs cannot be constructed; if the provided string is an invalid URL, it is rejected with an error.

Link copied to clipboard

Describes the static API surface available on the standard URL constructor. These methods are available for class execution without an object context.

Properties

Link copied to clipboard
abstract val hash: String

From MDN: "The hash property of the URL interface is a string containing a '#' followed by the fragment identifier of the URL. The fragment is not percent-decoded. If the URL does not have a fragment identifier, this property contains an empty string — ""."

Link copied to clipboard
abstract val host: String

From MDN: "The host property of the URL interface is a string containing the host, that is the hostname, and then, if the port of the URL is nonempty, a ':', followed by the port of the URL."

Link copied to clipboard
abstract val hostname: String

From MDN: "The hostname property of the URL interface is a string containing the domain name of the URL."

Link copied to clipboard
abstract val href: String

From MDN: "The href property of the URL interface is a string containing the whole URL."

Link copied to clipboard
abstract val origin: String

From MDN: "The origin read-only property of the URL interface returns a string containing the Unicode serialization of the origin of the represented URL.

Link copied to clipboard
abstract val password: String

From MDN: "The password property of the URL interface is a string containing the password specified before the domain name. If it is set without first setting the username property, it silently fails."

Link copied to clipboard
abstract val pathname: String

From MDN: "The pathname property of the URL interface represents a location in a hierarchical structure. It is a string constructed from a list of path segments, each of which is prefixed by a / character. If the URL has no path segments, the value of its pathname property will be the empty string."

Link copied to clipboard
abstract val port: Int?

From MDN: "The port property of the URL interface is a string containing the port number of the URL."

Link copied to clipboard
abstract val protocol: String

From MDN: "The protocol property of the URL interface is a string representing the protocol scheme of the URL, including the final ':'."

Link copied to clipboard
abstract val search: String

From MDN: "The search property of the URL interface is a search string, also called a query string, that is a string containing a '?' followed by the parameters of the URL."

Link copied to clipboard

From MDN: "The searchParams readonly property of the URL interface returns a URLSearchParams object allowing access to the GET decoded query arguments contained in the URL."

Link copied to clipboard
abstract val username: String

From MDN: "The username property of the URL interface is a string containing the username specified before the domain name."

Functions

Link copied to clipboard
abstract fun getMember(key: String): Any
Link copied to clipboard
open override fun getMemberKeys(): Array<String>
Link copied to clipboard
open override fun hasMember(key: String): Boolean
Link copied to clipboard
abstract fun putMember(key: String, value: Value)
Link copied to clipboard
open fun removeMember(key: String): Boolean
Link copied to clipboard
abstract fun toJSON(): String

Convert the URL to a JSON-compatible string; in practice, the return result is the same value as toString.

Link copied to clipboard
abstract override fun toString(): String

Generate a string representation of this URL object.