The Wayback Machine - https://web.archive.org/web/20240619113701/https://www.geeksforgeeks.org/web-api-url/
Open In App

Web API URL

Last Updated : 26 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Web API URL is used to access and manipulate URLs. URL stands for Uniform Resource Locator. A URL is a unique address which is pointing to the resource. The URL is essential for making HTTP requests for making interactions with the API and for getting or sending data.

We will explore the above topics with their description and understand them with suitable examples.

Concepts and Usage

The structuring and parsing of the URL follow the URL standard. A URL is composed of different parts. Some of the important parts are described below.

Syntax

https://www.index.html:443/file1/file2/resouce.html?id=val&password=val2#anchor

Components

Components

Descriptions

Scheme The HTTP part of the URL is the schema which indicates the protocol that the browser must follow to get the URL resource.
Domain Name The www.index.html part is the domain name. It indicates the server that is being requested in place of the domain name it can be an IP address.
Port The 443 part is the port number which is the internal gate to access resources.
Path to resource The path /file1/file2/resouce.html is the path to the resource on the server.
Parameters The ?id=val&password=val2 is the parameter part that is provided to a web server.
Anchor The #anchor is the anchor part. It indicates the part of the resource we want to access.

Accessing components of URL 

For a given URL, the object can be created & parsing can be done for the URL, along with facilitating access to its constituent portions through its properties quickly.

Syntax

// Creating the URL Object
let address = new URL("https://geeksforgeeks.com/login/username=val1&password=val2");

// Accessing the Host & Path Address
let host = address.host;
let path = address.pathname;

Here, we are trying to access the URL Object, Host, and Path Address for the given URL.

URL Modifications

To modify the value of the URLs that are represented by the object, we can simply add new values for them. For instance, from the below URL, we will modify the username and password, which will update the overall URL. Thus, the properties of the URL can be settable.

Syntax

// Changes the username and password attribute of the URL component.
let name = "geeks1";
let pswd = 'geek@123';
let url = new URL("https://geeksforgeeks.com/login");
url.username = name;
url.password = pswd;

Implementing the Queries

The search property of the URL component defines the portion of the query string. It allows one to look up the values of parameters. For instance, considering the below URL given, the search property of the URL component will be “username=val1&password=val2”. To search for the individual parameters, the URLSearchParams object’s get() method can be used.

Syntax

// The parameter username and password
// value which is pass with url.
let url = new URL("https://geeksforgeeks.com/login?username=val1&password=val2");
console.log( url.searchParams.get('username');
console.lgo(url.serachParams.get('password');

Example 1: In this example, we will parse the URL and see different parts of the URL. We will see the pathname, port number, and protocol.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>Web API URL</title>
</head>
  
<body style="text-align: center;">
    <h1 style="color:green">Welcome To GFG</h1>
    <h2>URL details</h2>
    <h2 id="ele"></h2>
</body>
<script>
    const url = new URL(document.location.href);
    document.getElementById("ele").innerHTML =
        '<h3>Path Name: ' + url.pathname +
        '</h3><h3>Port: ' + url.port +
        '</h3>' + '<h3> Protocol ' +
        url.protocol + '</h3>'
</script>
  
</html


Output:

url1

 

Example 2: In this example, we change the URL component attribute, see host, hostname, and origin attribute of url component.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>Web API URL</title>
</head>
  
<body style="text-align: center;">
    <h1 style="color:green">Welcome To GFG</h1>
    <p>
        <button onclick="fun()">Get host</button>
        <button onclick="fun2()">Get hostName</button>
        <button onclick="fun3()">Get Full URL</button>
        <button onclick="fun4()">Get UserName</button>
    </p>
    <h2 id="ele"></h2>
  
</body>
<script>
    const url = new URL(document.location.href);
    url.username = 'geeks1';
    url.password = 'geek@123';
    function fun() {
        document.getElementById('ele').innerHTML =
            '<h3>Host is : ' + url.host;
    }
    function fun2() {
        document.getElementById('ele').innerHTML =
            '<h3>Host Name is : ' + url.hostname;
    }
    function fun3() {
        document.getElementById('ele').innerHTML =
            '<h3>Full Url is : ' + url.origin;
    }
    function fun4() {
        document.getElementById('ele').innerHTML =
            '<h3>User Name is: ' + url.username;
    }
</script>
  
</html>


Output:

url2

Browser Support

  • Chrome 32
  • Edge 12
  • Firefox 19
  • Opera 19
  • Safari 7


Similar Reads

Web API URL.search Property
The Web API URL.search property is used to get USVString which is a search string or a query string. This string contains a "?" followed by the parameters of the URL. Syntax: var str = URL.search Return Value: This property returns a USVString search string. Example 1: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;body&gt; &lt;h1&gt;GeeksforGee
1 min read
Web API URL.protocol Property
The Web API URL.protocol property is used to get USVString containing the protocol scheme of the URL. Syntax: var str = URL.protocol Return Value: This property returns a USVString containing the protocol scheme of the URL. Example 1: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;body&gt; &lt;h1&gt;GeeksforGeeks&lt;/h1&gt; &lt;button onclick=
1 min read
Web API URL.port Property
The Web API URL.port property is used to get USVString containing the port number of the URL. Syntax: var str = URL.port Return Value: This property returns a USVString containing the port number of the URL. Example 1: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;body&gt; &lt;h1&gt;GeeksforGeeks&lt;/h1&gt; &lt;button onclick=&quot;get()&quot;
1 min read
Web API URL.pathname Property
The Web API URL.pathname property is used to get USVString containing an initial “/” followed by the path of the URL. Syntax: var str = URL.pathname Return Value: This property returns a USVString containing the pathname of the URL. Note: If there is no path set for the URL, so it will Return only /. Example 1: There is no path set for the URL, so
1 min read
Web API URL.username Property
The Web API URL.username property returns a USVString containing the username specified before the domain name of the URL Syntax: var str = URL.username Return Value: This property returns a USVString containing the username specified before the domain name of the URL. Example 1: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;body&gt; &lt;h1&gt;
1 min read
Web API URL.password Property
The Web API URL.password property returns a USVString containing the password specified before the domain name of the URL. A USVString is a sequence of Unicode scalar values. Syntax: var str = URL.password Return Value: This property returns a USVString containing the password specified before the domain name of the URL. Example 1: C/C++ Code &lt;!
1 min read
Web API URL.origin Property
The Web API URL.origin property returns a USVString containing the origin of the URL. Syntax: var str = URL.origin Return Value: This property returns a USVString containing the origin of the URL. Example 1: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;body&gt; &lt;h1&gt;GeeksforGeeks&lt;/h1&gt; &lt;div id=&quot;abc&quot;&gt;&lt;/div&gt; &lt;b
1 min read
Convert relative path URL to absolute path URL using JavaScript
Given a relative URL, the task is to convert the relative URL to an absolute URL. Here, the base URL is also given. 2 approaches are discussed here, the first example has the baseURL provided by the user and the second takes it from the URL of the page. Approach 1: Get the relURL and baseURL from user.Use .split() method to split the base and relat
3 min read
What is URL Locator & what are common schemes used for the URL in HTML ?
Every HTML document on the internet has a unique URL. URL stands for Uniform Resource Locator. The URL is the HTML document's address on the internet. Any file that can be accessed via the internet has a URL, including images, JavaScript files, CSS files, Flash files, and so on. To view an HTML document in a browser, enter the URL of the document i
2 min read
Differences between Web Services and Web API
Web Services: A Web services are any bit of services that makes it accessible over the Internet and normalizes its correspondence through XML encoding. A customer conjures web services by sending a solicitation (for the most part as an XML message), and the services send back an XML response. Web services summon communication over a network, with H
3 min read