URL
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
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.
Describes the static API surface available on the standard URL
constructor. These methods are available for class execution without an object context.
Properties
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."
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."