The URL API is a component of the URL standard, which defines what constitutes a valid Uniform Resource Locator and the API that accesses and manipulates URLs. The URL standard also defines concepts such as domains, hosts, and IP addresses, and also attempts to describe in a standard way the legacy application/x-www-form-urlencodedMIME type used to submit web forms' contents as a set of key/value pairs.
Concepts and usage
The majority of the URL standard is taken up by the definition of a URL and how it is structured and parsed. Also covered are definitions of various terms related to addressing of computers on a network, and the algorithms for parsing IP addresses and DOM addresses are specified. More interesting to most developers is the API itself.
Accessing URL components
Creating an URL object for a given URL parses the URL and provides quick access to its constituent parts through its properties.
js
let addr =newURL("https://developer.mozilla.org/en-US/docs/Web/API/URL_API");let host = addr.host;let path = addr.pathname;
The snippet above creates a URL object for the article you're reading right now, then fetches the host and pathname properties. In this case, those strings are developer.mozilla.org and /en-US/docs/Web/API/URL_API, respectively.
Changing the URL
Most of the properties of URL are settable; you can write new values to them to alter the URL represented by the object. For example, to create a URL and set its username:
js
let myUsername ="someguy";let addr =newURL("https://example.com/login");
addr.username = myUsername;
Setting the value of username not only sets that property's value, but it updates the overall URL. After executing the code snippet above, the value returned by href is https://someguy@example.com/login. This is true for any of the writable properties.
Queries
The search property on a URL contains the query string portion of the URL. For example, if the URL is https://example.com/login?user=someguy&page=news, then the value of the search property is ?user=someguy&page=news. You can also look up the values of individual parameters with the URLSearchParams object's get() method:
js
let addr =newURL("https://example.com/login?user=someguy&page=news");try{loginUser(addr.searchParams.get("user"));gotoPage(addr.searchParams.get("page"));}catch(err){showErrorMessage(err);}
For example, in the above snippet, the username and target page are taken from the query and passed to appropriate functions that are used by the site's code to log in and route the user to their desired destination within the site.
Other functions within URLSearchParams let you change the value of keys, add and delete keys and their values, and even sort the list of parameters.
Interfaces
The URL API is a simple one, with only a couple of interfaces to its name:
Defines utility methods to work with the query string of a URL.
Examples
If you want to process the parameters included in a URL, you could do it manually, but it's much easier to create a URL object to do it for you. The fillTableWithParameters() function below takes as input a HTMLTableElement object representing a <table>. Rows are added to the table, one for each key found in the parameters, with the first column containing the key's name, and the second column having the value.
Note the call to URLSearchParams.sort() to sort the parameter list before generating the table.
29Before version 57 single quotes in URLs were escaped (see bug 1386683).
36
10.1
49
29Before version 57 single quotes in URLs were escaped (see bug 1386683).
36
10.3
5.0
49
append
49
17
29
36
10.1
49
29
36
10.3
5.0
49
delete
49
17
29
36
1410.1Removing a non-existent query parameter doesn't remove ? from the URL. See bug 193022.
49
29
36
1410.3Removing a non-existent query parameter doesn't remove ? from the URL. See bug 193022.
5.0
49
entries
49
17
44
36
10.1
49
44
36
10.3
5.0
49
forEach
49
17
44
36
10.1
49
44
36
10.3
5.0
49
get
49
17
29
36
10.1
49
29
36
10.3
5.0
49
getAll
49
17
29
36
10.1
49
29
36
10.3
5.0
49
has
49
17
29
36
10.1
49
29
36
10.3
5.0
49
keys
49
17
44
36
10.1
49
44
36
10.3
5.0
49
set
49
17
29
36
10.1
49
29
36
10.3
5.0
49
size
113
113
112
99
17
113
112
76
17
23.0
113
sort
61
17
54
48
11
61
54
45
11
8.0
61
toString
49
17
29
36
10.1
49
29
36
10.3
5.0
49
values
49
17
44
36
10.1
49
44
36
10.3
5.0
49
Desktop
Mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on IOS
Samsung Internet
WebView Android
URL
19
12Before Edge 79, query arguments in the base URL argument are removed when calling the URL constructor.
26Before version 122, host, hostname, and port were not parsed for unknown protocols/schemes.
15
14.16Before Safari 14.1, calling the URL constructor with a base URL whose value is undefined caused Safari to throw a TypeError, see bug 216841.
25
26Before version 122, host, hostname, and port were not parsed for unknown protocols/schemes.
14
14.56Before Safari 14.1, calling the URL constructor with a base URL whose value is undefined caused Safari to throw a TypeError, see bug 216841.
1.5
4.4
URL_API
3219
12
19Before version 57, Firefox had a bug whereby single quotes contained in URLs are escaped when accessed via URL APIs (see bug 1386683).
1915
76
3225
19Before version 57, Firefox had a bug whereby single quotes contained in URLs are escaped when accessed via URL APIs (see bug 1386683).
1914
76
2.01.5
4.44
canParse_static
120
120
115
106
17
120
115
80
17
25.0
120
createObjectURL_static
19
12
19createObjectURL() is no longer available within the context of a ServiceWorker.
15
6
25
19createObjectURL() is no longer available within the context of a ServiceWorker.
14
6
1.5
4.4
hash
32
13
22
19
7
32
22
19
7
2.0
4.4.3
host
32
13
22
19
7
32
22
19
7
2.0
4.4.3
hostname
32
13
22
19
10
32
22
19
10
2.0
4.4.3
href
32
13
22
19
10
32
22
19
10
2.0
4.4.3
origin
32
12
2626–49Results for URL using the blob scheme incorrectly returned null.
19
10
32
2626–49Results for URL using the blob scheme incorrectly returned null.
19
10
6.0
4.4.3
parse_static
126
126
126
112
18
126
126
83
18
No
126
password
32
12
26
19
10
32
26
19
10
6.0
4.4.3
pathname
32
13
22Before Firefox 53, pathname and search returned wrong values for custom protocols. Given protocol:host/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. See bug 1310483.
19
10
32
22Before Firefox 53, pathname and search returned wrong values for custom protocols. Given protocol:host/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. See bug 1310483.
19
10
2.0
4.4.3
port
32
13
22
19
10
32
22
19
10
2.0
4.4.3
protocol
32
13
22
19
10
32
22
19
10
2.0
4.4.3
revokeObjectURL_static
19
12
19revokeObjectURL() is no longer available within the context of a ServiceWorker.
15
6
25
19revokeObjectURL() is no longer available within the context of a ServiceWorker.
14
6
1.5
4.4
search
32
13
22Before Firefox 53, pathname and search returned wrong values for custom protocols. Given protocol:host/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. See bug 1310483.
19
10
32
22Before Firefox 53, pathname and search returned wrong values for custom protocols. Given protocol:host/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. See bug 1310483.