How to get protocol, domain and port from URL using JavaScript ? Last Updated : 30 May, 2019 Comments Improve Suggest changes Like Article Like Report The protocol, domain, and port of the current page can be found by two methods: Method 1: Using location.protocol, location.hostname, location.port methods: The location interface has various methods that can be used to return the required properties. The location.protocol property is used to return the protocol scheme of the URL along with the final colon(:). The location.hostname is used to return the domain name of the URL. The location.port property is used to return the port of the URL. It returns nothing if the port is not described explicitly in the URL. Syntax: protocol = location.protocol; domain = location.hostname; port = location.port; Example: html <!DOCTYPE html> <html> <head> <title> Get protocol, domain, and port from URL </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> Get protocol, domain, and port from URL </b> <p> Protocol is: <span class="protocol"></span> </p> <p> Domain is: <span class="domain"></span> </p> <p> Port is: <span class="port"></span> </p> <button onclick="getDetails()"> Get protocol, domain, port </button> <script type="text/javascript"> function getDetails() { protocol = location.protocol; domain = location.hostname; port = location.port; document.querySelector('.protocol').textContent = protocol; document.querySelector('.domain').textContent = domain; document.querySelector('.port').textContent = port; } </script> </body> </html> Output: Before Clicking the button: After Clicking the button: Method 2: Using the URL interface: The URL interface is used to represent object URL. It can be used for getting the port, domain, and protocol as it has inbuilt methods to get these values. The url.protocol property is used to return the protocol scheme of the URL along with the final colon(:). The url.hostname is used to return the domain of the URL. The url.port property is used to return the port of the URL. It returns '' if the port is not described explicitly. Note: This API is not supported in Internet Explorer 11. Syntax: current_url = window.location.href; url_object = new URL(current_url); protocol = url_object.protocol; domain = url_object.hostname; port = url_object.port; Example: html <!DOCTYPE html> <html> <head> <title> Get protocol, domain, and port from URL </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> Get protocol, domain, and port from URL </b> <p>Protocol is: <span class="protocol"></span></p> <p>Domain is: <span class="domain"></span></p> <p>Port is: <span class="port"></span></p> <button onclick="getDetails()"> Get protocol, domain, port </button> <script type="text/javascript"> function getDetails() { current_url = window.location.href; url_object = new URL(current_url); protocol = url_object.protocol; domain = url_object.hostname; port = url_object.port; document.querySelector('.protocol').textContent = protocol; document.querySelector('.domain').textContent = domain; document.querySelector('.port').textContent = port; } </script> </body> </html> Output: Before Clicking the button: After Clicking the button: Comment More infoAdvertise with us Next Article How to get protocol, domain and port from URL using JavaScript ? sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Get Domain Name From URL in JavaScript? In JavaScript, the URL object allows you to easily parse URLs and access their components. This is useful when you need to extract specific parts of a URL, such as the domain name. The hostname property of the URL object provides the domain name of the URL.PrerequisiteJavascriptHTMLBelow are the fol 2 min read How to get the file name from page URL using JavaScript ? JavaScript provides multiple techniques for string manipulation and pattern matching. By demonstrating various methods, the article equips developers with versatile solutions to dynamically retrieve and utilize file names from different URL formats within their applications. There are several approa 3 min read How to get URL Parameters using JavaScript ? To get URL parameters using JavaScript means extracting the query string values from a URL. URL parameters, found after the ? in a URL, pass data like search terms or user information. JavaScript can parse these parameters, allowing you to programmatically access or manipulate their values.For getti 3 min read How to Extract the Host Name from URL using JavaScript? Extracting the hostname from a URL using JavaScript means retrieving the domain part from a complete web address. This can be done using JavaScript's URL object or methods like window.location, which allow easy access to the hostname of a URL.What is URL?A URL (Uniform Resource Locator) is the web a 2 min read How To Get URL And URL Parts In JavaScript? In web development, working with URLs is a common task. Whether we need to extract parts of a URL or manipulate the URL for navigation, JavaScript provides multiple approaches to access and modify URL parts. we will explore different approaches to retrieve the full URL and its various components.The 3 min read How to Get the Current URL using JavaScript? Here are two different methods to get the current URL in JavaScript.1. Using Document.URL PropertyThe DOM URL property in HTML is used to return a string that contains the complete URL of the current document. The string also includes the HTTP protocol such as ( http://).Syntaxdocument.URLReturn Val 1 min read How to parse URL using JavaScript ? Given an URL and the task is to parse that URL and retrieve all the related data using JavaScript. Example: URL: https://www.geeksforgeeks.org/courses When we parse the above URL then we can find hostname: geeksforgeeks.com path: /courses Method 1: In this method, we will use createElement() method 2 min read How to get an object containing parameters of current URL in JavaScript ? The purpose of this article is to get an object which contains the parameter of the current URL. Example: Input: www.geeksforgeeks.org/search?name=john&age=27 Output: { name: "john", age: 27 } Input: geeksforgeeks.org Output: {} To achieve this, we follow the following steps. Create an empty obj 2 min read How to parse a URL into hostname and path in javascript ? We can parse a URL(Uniform Resource Locator) to access its component and this can be achieved using predefined properties in JavaScript. For Example, the first example parses the URL of the current web page and the second example parses a predefined URL. Example 1:This example parses the URL of the 1 min read How to get client IP address using JavaScript? Imagine your computer as your apartment. Just like each apartment has a unique address for receiving mail, your computer has an IP address that helps it receive information from the internet. This IP address acts like a label that identifies your specific device on the vast network of computers, ens 2 min read Like