You’re reading the English version of this content since no translation exists yet for this locale. Help us translate this article!

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, a DOMException of type SYNTAX_ERROR is thrown.

Note: This feature is available in Web Workers.


url = new URL(url, [base])


A USVString representing an absolute or relative URL. If url is a relative URL, base is required, and will be used as the base URL. If url is an absolute URL, a given base will be ignored.
base Optional
A USVString representing the base URL to use in case url is a relative URL. If not specified, it defaults to ''.

Note: You can still use an existing URL object for the base, which stringifies itself to the object's href attribute.


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


// Base urls
var m = 'https://developer.mozilla.org';
var a = new URL("/", m);                                // => 'https://developer.mozilla.org/'
var b = new URL(m);                                     // => 'https://developer.mozilla.org/'

        new URL('en-US/docs', b);                      // => 'https://developer.mozilla.org/en-US/docs'
var 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'
        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
        new URL('http://www.example.com', );           // => 'http://www.example.com/'
        new URL('http://www.example.com', b);          // => 'http://www.example.com/'

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


Specification Status Comment
The definition of 'URL.URL()' in that specification.
Living Standard Initial definition.

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
URL() constructorChrome Full support YesEdge Full support 12Firefox Full support 26IE No support ? — 11Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 26Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes


Full support  
Full support
No support  
No support

See also

  • The interface it belongs to: URL.