A URL (Uniform Resource Locator) is a reference or address used to access resources on the internet. It specifies the location of a resource as well as the protocol used to retrieve it.
A URL consists of several components, including the protocol, domain name, port (optional), path, query string (optional), and fragment (optional). Each part provides specific information about how to access the resource.
Example: Here is an example of a URL and its components, followed by how to work with URLs in JavaScript:
https://www.example.com:8080/path/to/resource?query=example#fragment
- Protocol:
https
- The protocol used to access the resource (in this case, HTTPS). - Domain Name:
www.example.com
- The domain name of the server where the resource is hosted. - Port:
8080
- The port number on the server (optional and often omitted if using default ports for HTTP/HTTPS). - Path:
/path/to/resource
- The path to the resource on the server. - Query String:
?query=example
- Optional parameters sent to the server. - Fragment:
#fragment
- A fragment identifier for a specific section of the resource (optional).