How to get protocol, domain and port from URL using JavaScript ? Last Updated : 30 May, 2019 Summarize Comments Improve Suggest changes Share 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 ? S 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 Like