URL: URL() constructor

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2021.

Note: This feature is available in Web Workers.

The URL() constructor returns a newly created URL object representing the URL defined by the parameters.

If the given base URL or the resulting URL are not valid URLs, the JavaScript TypeError exception is thrown.

Syntax

js
new URL(url)
new URL(url, base)

Parameters

url

A string or any other object with a stringifier that represents an absolute URL or a relative reference to a base URL. If url is a relative reference, base is required, and is used to resolve the final URL. If url is an absolute URL, a given base will not be used to create the resulting URL.

base Optional

A string representing the base URL to use in cases where url is a relative reference. If not specified, it defaults to undefined.

When a base is specified, the resolved URL is not simply a concatenation of url and base. Relative references to the parent and current directory are resolved relative to the current directory of the base URL, which includes path segments up until the last forward-slash, but not any after. Relative references to the root are resolved relative to the base origin. For more information see Resolving relative references to a URL.

Note: The url and base arguments will each be stringified from whatever value you pass, such as an HTMLAnchorElement or HTMLAreaElement element, just like with other Web APIs that accept a string. In particular, you can use an existing URL object for either argument, and it will be stringified from the object's href property.

Exceptions

TypeError

url (in the case of absolute URLs) or base + url (in the case of relative references) is not a valid URL.

Examples

Here are some examples of using the constructor.

Note: Resolving relative references to a URL provides additional examples demonstrating how different url and base values are resolved to a final absolute URL.

js
// Base URLs:
let baseUrl = "https://developer.mozilla.org";

let a = new URL("/", baseUrl);
// => 'https://developer.mozilla.org/'

let b = new URL(baseUrl);
// => 'https://developer.mozilla.org/'

new URL("en-US/docs", b);
// => 'https://developer.mozilla.org/en-US/docs'

let d = new URL("/en-US/docs", b);
// => 'https://developer.mozilla.org/en-US/docs'

new URL("/en-US/docs", d);
// => 'https://developer.mozilla.org/en-US/docs'

new URL("/en-US/docs", a);
// => 'https://developer.mozilla.org/en-US/docs'

new URL("/en-US/docs", "https://developer.mozilla.org/fr-FR/toto");
// => 'https://developer.mozilla.org/en-US/docs'

Here are some examples of invalid URLs:

js
new URL("/en-US/docs", "");
// Raises a TypeError exception as '' is not a valid URL

new URL("/en-US/docs");
// Raises a TypeError exception as '/en-US/docs' is not a valid URL

// Other cases:

new URL("http://www.example.com");
// => 'http://www.example.com/'

new URL("http://www.example.com", B);
// => 'http://www.example.com/'

new URL("", "https://example.com/?query=1");
// => 'https://example.com/?query=1' (Edge before 79 removes query arguments)

new URL("/a", "https://example.com/?query=1");
// => 'https://example.com/a' (see relative URLs)

new URL("//foo.example", "https://example.com");
// => 'https://foo.example/' (see relative URLs)

Specifications

Specification
URL
# dom-url-url

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
URL() constructor
Chrome – Full support
Chrome 19 (Release date: 2012-05-15)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote
footnote Before Edge 79, query arguments in the base URL argument are removed when calling the URL constructor.
Firefox – Full support
Firefox 26 (Release date: 2013-12-10)
footnote
footnote Before version 122, host, hostname, and port were not parsed for unknown protocols/schemes.
Opera – Full support
Opera 15 (Release date: 2013-07-02)
footnote Full support
Safari – Partial support
Safari 6 – 14 (Release date: 2012-07-25)
footnote Partial support
footnote Before Safari 14.1, calling the URL constructor with a base URL whose value is undefined caused Safari to throw a TypeError, see bug 216841.
Safari – Full support
Safari 14.1 (Release date: 2021-04-26)
footnote Full support
Chrome Android – Full support
Chrome Android 25 (Release date: 2013-02-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 26 (Release date: 2013-12-10)
footnote
footnote Before version 122, host, hostname, and port were not parsed for unknown protocols/schemes.
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
footnote Full support
Safari on iOS – Partial support
Safari on iOS 6 – 14 (Release date: 2012-09-10)
footnote Partial support
footnote Before Safari on iOS 14.1, calling the URL constructor with a base URL whose value is undefined caused Safari on iOS to throw a TypeError, see bug 216841.
Safari on iOS – Full support
Safari on iOS 14.5 (Release date: 2021-04-26)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1.5 (Release date: 2013-09-25)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Partial support
WebView on iOS 6 – 14 (Release date: 2012-09-10)
footnote Partial support
footnote Before WebView on iOS 14.1, calling the URL constructor with a base URL whose value is undefined caused WebView on iOS to throw a TypeError, see bug 216841.
WebView on iOS – Full support
WebView on iOS 14.5 (Release date: 2021-04-26)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 6.13 (Release date: 2018-02-13)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
See implementation notes.
Has more compatibility info.

See also