Convert relative path URL to absolute path URL using JavaScript Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 relative URL on "/" and get each portion in the array, st, and arr respectively.Run a loop on arr length and for each turn, If the arr[i] == '..' then pop the element from an st array, else push the arr[i] in an st array using .push() and .pop() method.Join the st array using .join() method on "/" to get the absolute URL. Example 1: This example implements the above approach. html <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> </head> <body> <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"> </p> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="color: green;"> </p> <script> var up = document.getElementById('GFG_UP'); var element = document.getElementById("body"); var base = "https://www.geeksforgeeks.org/folder/"; var relURL = "./fileName.txt"; up.innerHTML = "Click on the button to convert relative path"+ " URL to absolute path URL.<br><br>BaseURL = '" + base + "'<br>Relative URL = '" + relURL + "'"; function absolute(base, rel) { var st = base.split("/"); var arr = rel.split("/"); st.pop(); // ignore the current file name (or no string) // (ignore if "base" is the current folder without having slash in trail) for (var i = 0; i < arr.length; i++) { if (arr[i] == ".") continue; if (arr[i] == "..") st.pop(); else st.push(arr[i]); } return st.join("/"); } function GFG_Fun() { $('#GFG_DOWN').html(absolute(base, relURL)); } </script> </body> Output: Convert relative path URL to absolute path URL using JavaScript Approach 2: Get the relURL from user.Create an anchor element using document.createElement("a") and set the href attribute equal to the relURL.Use link.protocol, link.host and link.pathname to get the protocol, hostName and pathname(relURL) of the page respectively. Example 2: This example implements the above approach. html <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> </head> <body> <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"> </p> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="color: green;"> </p> <script> var up = document.getElementById('GFG_UP'); var element = document.getElementById("body"); var relURL = "./fileName.txt"; up.innerHTML = "Click on the button to convert relative path URL to"+ " absolute path URL.<br><br>Relative URL = '" + relURL + "'"; var absolute = function(rel) { var link = document.createElement("a"); link.href = rel; return (link.protocol + "//" + link.host + link.pathname); } function GFG_Fun() { $('#GFG_DOWN').html(absolute(relURL)); } </script> </body> Output: Convert relative path URL to absolute path URL using JavaScript Comment More infoAdvertise with us Next Article How to get the file name from page URL using JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads 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 JavaScript Check whether a URL string is absolute or relative In this article, the task is to check if the passed URL is absolute or relative. Below are a few approaches: Approaches to Check if URL is Absolute or Relative:Table of Content Approach 1: Using JavaScript Regular ExpressionApproach 2: Using JavaScript string.indexof() methodApproach 3: Using JavaSc 2 min read JavaScript Check whether a URL string is absolute or relative In this article, the task is to check if the passed URL is absolute or relative. Below are a few approaches: Approaches to Check if URL is Absolute or Relative:Table of Content Approach 1: Using JavaScript Regular ExpressionApproach 2: Using JavaScript string.indexof() methodApproach 3: Using JavaSc 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 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 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 Like