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.
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.
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.
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 ="some-guy";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://some-guy@example.com/login. This is true for any of the writable properties.
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=some-guy&page=news, then the value of the search property is ?user=some-guy&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=some-guy&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.
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.
Tip: you can click/tap on a cell for more information.
Full support
Full support
Partial support
Partial support
No support
No support
See implementation notes.
Requires a vendor prefix or different name for use.
Has more compatibility info.
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Tip: you can click/tap on a cell for more information.
Full support
Full support
Partial support
Partial support
No support
No support
See implementation notes.
Has more compatibility info.
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.