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 interface represents an object providing static methods used for creating object URLs.

When using a user agent where no constructor has been implemented yet, it is possible to access such an object using the Window.URL properties (prefixed with Webkit-based browser as Window.webkitURL).

Note: This feature is available in Web Workers.

Usage

URL is used to parse, construct, normalise, and encode URLs.

The constructor takes a url parameter, and an optional base parameter to use as a base if the url parameter is a relative URL:

const url = new URL('../cats', 'http://www.example.com/dogs');
console.log(url.hostname); // "www.example.com"
console.log(url.pathname); // "/cats"

URL properties can be set to construct the URL:

url.hash = 'tabby';
console.log(url.href); // "http://www.example.com/cats#tabby"

URLs will be encoded as per RFC 3986:

url.pathname = 'démonstration.html';
console.log(url.href); // "http://www.example.com/d%C3%A9monstration.html"

The URLSearchParams interface can be used to build and manipulate the URL query string.

To get the search params from the current window's URL, you can do this:

// https://some.site/?id=123
var parsedUrl = new URL(window.location.href);
console.log(parsedUrl.searchParams.get("id")); // 123

The toString method of URL is the href property, so the constructor can be used to normalise and encode a URL directly.

const response = await fetch(new URL('http://www.example.com/démonstration.html'));

Constructor

URL()
Creates and return a URL object composed from the given parameters.

Properties

URL.hash
Is a DOMString containing a '#' followed by the fragment identifier of the URL.
URL.host
Is a DOMString containing the domain (that is the hostname) followed by (if a port was specified) a ':' and the port of the URL.
URL.hostname
Is a DOMString containing the domain of the URL.
URL.href
Is a DOMString containing the whole URL.
URL.origin Read only
Returns a DOMString containing the origin of the URL, that is its scheme, its domain and its port.
URL.password
Is a DOMString containing the password specified before the domain name.
URL.pathname
Is a DOMString containing an initial '/' followed by the path of the URL.
URL.port
Is a DOMString containing the port number of the URL.
URL.protocol
Is a DOMString containing the protocol scheme of the URL, including the final ':'.
URL.search
Is a DOMString containing a '?' followed by the parameters of the URL.
URL.searchParams Read only
Returns a URLSearchParams object allowing to access the GET query arguments contained in the URL.
URL.username
Is a DOMString containing the username specified before the domain name.
Methods

The URL interface implements methods defined in URLUtils.

URLUtils.toString()
Returns a DOMString containing the whole URL. It is a synonym for URLUtils.href, though it can't be used to modify the value.
URL.toJSON() [available since Firefox v54]
Returns a DOMString  containing the whole URL. It returns the same string as the href property.

Static methods

URL.createObjectURL()
Returns a DOMString containing a unique blob URL, that is a URL with blob: as its scheme, followed by an opaque string uniquely identifying the object in the browser.
URL.revokeObjectURL()
Revokes an object URL previously created using URL.createObjectURL().

Specifications

Specification Status Comment
File API
The definition of 'URL' in that specification.
Working Draft Added the static methods URL.createObjectURL() and URL.revokeObjectURL().
URL
The definition of 'API' in that specification.
Living Standard Initial definition (implements URLUtils).

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome Full support 32
Full support 32
Full support 2
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Full support 12Firefox Full support 19
Notes
Full support 19
Notes
Notes Firefox had a bug whereby single quotes contained in URLs are escaped when accessed via URL APIs (see bug 1386683). This has been fixed as of Firefox 57.
Notes To use it from chrome code, JSM and Bootstrap scope, you have to import it with Cu.importGlobalProperties(['URL']);.
No support 4 — 18
Notes
Notes This interface was supported with the non-standard nsIDOMMozURLProperty internal type. However this didn't make any difference in practice, as the only way to access such an object was through window.URL.
IE ? Opera Full support 19
Full support 19
Full support 15
Prefixed
Prefixed Requires the vendor prefix: webkit
Safari Full support 7
Full support 7
Full support 6
Prefixed
Prefixed Requires the vendor prefix: webkit
WebView Android Full support 4.4
Full support 4.4
Full support 4
Prefixed
Prefixed Requires the vendor prefix: webkit
Chrome Android Full support 32
Full support 32
Full support 18
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Mobile Full support 12Firefox Android Full support 19
Notes
Full support 19
Notes
Notes Firefox had a bug whereby single quotes contained in URLs are escaped when accessed via URL APIs (see bug 1386683). This has been fixed as of Firefox 57.
Notes To use it from chrome code, JSM and Bootstrap scope, you have to import it with Cu.importGlobalProperties(['URL']);.
No support 14 — 18
Notes
Notes This interface was supported with the non-standard nsIDOMMozURLProperty internal type. However this didn't make any difference in practice, as the only way to access such an object was through window.URL.
Opera Android Full support 19
Full support 19
Full support 15
Prefixed
Prefixed Requires the vendor prefix: webkit
Safari iOS Full support 7
Full support 7
Full support 6
Prefixed
Prefixed Requires the vendor prefix: webkit
Samsung Internet Android ?
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 YesEdge Mobile Full support 12Firefox Android Full support 26Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
createObjectURL
Experimental
Chrome Full support 8Edge Full support 12Firefox Full support 4
Notes
Full support 4
Notes
Notes createObjectURL() is no longer available within the context of a ServiceWorker.
IE Full support 10Opera Full support 15Safari Full support 6WebView Android ? Chrome Android Full support 18Edge Mobile ? Firefox Android Full support 4
Notes
Full support 4
Notes
Notes createObjectURL() is no longer available within the context of a ServiceWorker.
Opera Android Full support 15Safari iOS Full support 6Samsung Internet Android ?
hashChrome Full support YesEdge Full support 13Firefox Full support 22IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 22Opera Android ? Safari iOS ? Samsung Internet Android ?
hostChrome Full support YesEdge Full support 13Firefox Full support 22IE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 22Opera Android ? Safari iOS ? Samsung Internet Android ?
hostnameChrome Full support YesEdge Full support 13Firefox Full support 22IE ? Opera Full support YesSafari Full support 10WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 22Opera Android ? Safari iOS ? Samsung Internet Android ?
hrefChrome Full support YesEdge Full support 13Firefox Full support 22IE ? Opera Full support YesSafari Full support 10WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 22Opera Android ? Safari iOS ? Samsung Internet Android ?
originChrome Full support 52Edge Full support 12
Notes
Full support 12
Notes
Notes Defined in another interface but available to use from URL.
Firefox Full support 26
Full support 26
No support 26 — 44
Notes
Notes This property was on the URLUtils mixin. It has been moved either to the HTMLHyperlinkElementUtils mixin, or directly on the interface.
No support 26 — 49
Notes
Notes Results for URL using the blob scheme incorrectly returned null.
IE ? Opera Full support Yes
Notes
Full support Yes
Notes
Notes Defined in another interface but available to use from URL.
Safari Full support 10
Notes
Full support 10
Notes
Notes Defined in another interface but available to use from URL.
WebView Android Full support 52Chrome Android Full support 52Edge Mobile Full support 12
Notes
Full support 12
Notes
Notes Defined in another interface but available to use from URL.
Firefox Android Full support 26
Full support 26
No support 26 — 44
Notes
Notes This property was on the URLUtils mixin. It has been moved either to the HTMLHyperlinkElementUtils mixin, or directly on the interface.
No support 26 — 49
Notes
Notes Results for URL using the blob scheme incorrectly returned null.
Opera Android Full support Yes
Notes
Full support Yes
Notes
Notes Defined in another interface but available to use from URL.
Safari iOS Full support Yes
Notes
Full support Yes
Notes
Notes Defined in another interface but available to use from URL.
Samsung Internet Android ?
passwordChrome Full support 52Edge Full support 12
Notes
Full support 12
Notes
Notes Defined in another interface but available to use from URL.
Firefox Full support 26IE ? Opera Full support Yes
Notes
Full support Yes
Notes
Notes Defined in another interface but available to use from URL.
Safari Full support 10
Notes
Full support 10
Notes
Notes Defined in another interface but available to use from URL.
WebView Android Full support 52Chrome Android Full support 52Edge Mobile Full support 12
Notes
Full support 12
Notes
Notes Defined in another interface but available to use from URL.
Firefox Android Full support 26Opera Android Full support Yes
Notes
Full support Yes
Notes
Notes Defined in another interface but available to use from URL.
Safari iOS Full support Yes
Notes
Full support Yes
Notes
Notes Defined in another interface but available to use from URL.
Samsung Internet Android ?
pathnameChrome Full support YesEdge Full support 13Firefox Full support 53
Full support 53
No support 22 — 53
Notes
Notes pathname and search returned the wrong values so that for a URL of http://z.com/x?a=true&b=false, pathname would return "/x?a=true&b=false" and search would return "", rather than "/x" and "?a=true&b=false" respectively.
IE ? Opera Full support YesSafari Full support 10WebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android Full support 53
Full support 53
No support 22 — 53
Notes
Notes pathname and search returned the wrong values so that for a URL of http://z.com/x?a=true&b=false, pathname would return "/x?a=true&b=false" and search would return "", rather than "/x" and "?a=true&b=false" respectively.
Opera Android ? Safari iOS ? Samsung Internet Android ?
portChrome Full support YesEdge Full support 13Firefox Full support 22IE ? Opera Full support YesSafari Full support 10WebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android Full support 22Opera Android ? Safari iOS ? Samsung Internet Android ?
protocolChrome Full support YesEdge Full support 13Firefox Full support 22IE ? Opera Full support YesSafari Full support 10WebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android Full support 22Opera Android ? Safari iOS ? Samsung Internet Android ?
revokeObjectURL
Experimental
Chrome Full support 8Edge Full support 12Firefox Full support 4
Notes
Full support 4
Notes
Notes revokeObjectURL() is no longer available within the context of a ServiceWorker.
IE Full support 10Opera Full support 15Safari Full support 6WebView Android ? Chrome Android Full support 18Edge Mobile ? Firefox Android Full support 4
Notes
Full support 4
Notes
Notes revokeObjectURL() is no longer available within the context of a ServiceWorker.
Opera Android Full support 15Safari iOS Full support 6Samsung Internet Android ?
searchChrome Full support YesEdge Full support 13Firefox Full support 53
Full support 53
No support 22 — 53
Notes
Notes pathname and search returned the wrong values so that for a URL of http://z.com/x?a=true&b=false, pathname would return "/x?a=true&b=false" and search would return "", rather than "/x" and "?a=true&b=false" respectively.
IE ? Opera Full support YesSafari Full support 10WebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android Full support 53
Full support 53
No support 22 — 53
Notes
Notes pathname and search returned the wrong values so that for a URL of http://z.com/x?a=true&b=false, pathname would return "/x?a=true&b=false" and search would return "", rather than "/x" and "?a=true&b=false" respectively.
Opera Android ? Safari iOS ? Samsung Internet Android ?
searchParamsChrome Full support 51Edge Full support 17Firefox Full support 52IE No support NoOpera Full support YesSafari Full support 10WebView Android Full support 51Chrome Android Full support 51Edge Mobile ? Firefox Android Full support 52Opera Android ? Safari iOS ? Samsung Internet Android ?
toJSONChrome ? Edge Full support 17Firefox Full support 54IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Edge Mobile ? Firefox Android Full support 54Opera Android ? Safari iOS ? Samsung Internet Android ?
usernameChrome Full support 52Edge Full support 12
Notes
Full support 12
Notes
Notes Defined in another interface but available to use from URL.
Firefox Full support 26IE ? Opera Full support Yes
Notes
Full support Yes
Notes
Notes Defined in another interface but available to use from URL.
Safari Full support 10
Notes
Full support 10
Notes
Notes Defined in another interface but available to use from URL.
WebView Android Full support 52Chrome Android Full support 52Edge Mobile Full support 12
Notes
Full support 12
Notes
Notes Defined in another interface but available to use from URL.
Firefox Android Full support 26Opera Android Full support Yes
Notes
Full support Yes
Notes
Notes Defined in another interface but available to use from URL.
Safari iOS Full support Yes
Notes
Full support Yes
Notes
Notes Defined in another interface but available to use from URL.
Samsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

See also

Tag del documento e collaboratori

Ultima modifica di: rugk,