How to parse URL using JavaScript ? Last Updated : 23 Jan, 2023 Comments Improve Suggest changes Like Article Like Report 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 to create a HTML element, anchor tag and then use it for parsing the given URL. javascript // Store the URL into variable var url = "https://geeksforgeeks.org/pathname/?search=query"; // Created a parser using createElement() method var parser = document.createElement("a"); parser.href = url; // Host of the URL console.log(parser.host); // Hostname of the URL console.log(parser.hostname ); // Pathname of URL console.log(parser.pathname); // Search in the URL console.log(parser.search ); Output: geeksforgeeks.org geeksforgeeks.org /pathname/ ?search=query Method 2: In this method we will use URL() to create a new URL object and then use it for parsing the provided URL. javascript // Store the URL into variable var url = "https://geeksforgeeks.org:3000/pathname/?search=query"; // Created a URL object using URL() method var parser = new URL(url); // Protocol used in URL console.log(parser.protocol); // Host of the URL console.log(parser.host); // Port in the URL console.log(parser.port); // Hostname of the URL console.log(parser.hostname); // Search in the URL console.log(parser.search); // Search parameter in the URL console.log(parser.searchParams); Output: https: geeksforgeeks.org:3000 3000 geeksforgeeks.org ?search=query search=query Comment More infoAdvertise with us Next Article How to parse URL using JavaScript ? R Rajnis09 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads 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 Parse XML in JavaScript? Parsing XML data is important because it allows JavaScript applications to extract structured information from XML documents. We will explore two different approaches to Parse XML in JavaScript. Below are the approaches to parsing XML in JavaScript: Table of Content Using DOM ParserUsing xml2js Libr 2 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 replace plain URL with link using JavaScript ? Given a plane URL, the task is to replace the plain URLs with the links. This problem can be solved with the help of Regular Expressions. Approach:Â Using RegExp and replace() MethodRegExp - This looks for the URL in the provided text.This RegExp parses the URL and puts the address to the $1 variable 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 Convert Title to URL Slug using JavaScript ? Converting a title to a URL slug in JavaScript involves transforming the input text into a format suitable for URLs. This typically entails replacing spaces with dashes, removing special characters, and ensuring lowercase consistency for compatibility and readability in web addresses.Prerequisite HT 2 min read How to Parse JSON in JavaScript ? Parse JSON in JavaScript, accepting a JSON string as input and returning a corresponding JavaScript object with two methods, using JSON.parse() for parsing JSON strings directly and employing the fetch API to parse JSON responses from web APIs. These techniques are crucial for seamless data manipula 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 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 JavaScript String substring() Method The substring() method in JavaScript is used to extract characters between two indices from a string, without modifying the original string. This method returns a new string that is a part of the original string, starting from a specified position up to (but not including) another specified position 5 min read Like