I volontari di MDN non hanno ancora tradotto questo articolo in Italiano. Unisciti a noi e traducilo tu stesso.
Puoi anche consultare l’articolo in English (US).

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.


var a = new URL("/", "https://developer.mozilla.org"); // Creates a URL pointing to 'https://developer.mozilla.org/'
var b = new URL("https://developer.mozilla.org");      // Creates a URL pointing to 'https://developer.mozilla.org/'
var c = new URL('en-US/docs', b);                      // Creates a URL pointing to 'https://developer.mozilla.org/en-US/docs'
var d = new URL('/en-US/docs', b);                     // Creates a URL pointing to 'https://developer.mozilla.org/en-US/docs'
var f = new URL('/en-US/docs', d);                     // Creates a URL pointing to 'https://developer.mozilla.org/en-US/docs'
var g = new URL('/en-US/docs', "https://developer.mozilla.org/fr-FR/toto");
                                                       // Creates a URL pointing to 'https://developer.mozilla.org/en-US/docs'
var h = new URL('/en-US/docs', a);                     // Creates a URL pointing to 'https://developer.mozilla.org/en-US/docs'
var i = new URL('/en-US/docs', '');                    // Raises a TypeError exception as '' is not a valid URL
var j = new URL('/en-US/docs');                        // Raises a TypeError exception as '/en-US/docs' is not a valid URL
var k = new URL('http://www.example.com', 'https://developers.mozilla.com');
                                                       // Creates a URL pointing to 'http://www.example.com/'
var l = new URL('http://www.example.com', b);          // Creates a URL pointing to 'http://www.example.com/'


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 AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome 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 YesEdge Mobile Full support 12Firefox Android Full support 26Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?


Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

See also

  • The interface it belongs to: URL.


Tag del documento e collaboratori

Hanno collaborato alla realizzazione di questa pagina: fscholz, GuilloOme, DevelX, chrisdavidmills, gonchuki, hsource, onlywei, yan, graingert, teoli, afreen, realityking, Bountin, mattbrundage, vishu_gawli
Ultima modifica di: fscholz,