JavaScript method to get the URL without query string Last Updated : 30 May, 2019 Comments Improve Suggest changes Like Article Like Report The task is to get the URL name of the page without using a query string with the help of JavaScript. replace() method: This method searches a string for a defined value, or a regular expression, and returns a new string with the replaced defined value. Syntax: string.replace(searchVal, newvalue) Parameters: searchVal: This parameter is required. It specifies the value, or regular expression, that is going to replace by the new value. newvalue: This parameter is required. It specifies the value to replace with the search value. Return value: It returns a new string where the defines value(s) has been replaced by the new value. split() method: This method is used to split a string into an array of substrings, and returns the new array. Syntax: string.split(separator, limit) Parameters: separator: This parameter is optional. It specifies the character, or the regular expression, to use for splitting the string. If not used, the whole string will be returned (an array with only one item). limit: This parameter is optional. It specifies the integer that specifies the number of splits, items beyond the split limit will be excluded from the array. Return value: It returns a new Array, having the splitted items. Example 1: This example first extracts the all URL's of the page using href and then gets the first URL by setting index = 0 and then removes the portion after ? using split() method. html <!DOCTYPE HTML> <html> <head> <title> JavaScript method to get the URL without query string </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button onclick = "GFG_click()"> Get URL </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); el_up.innerHTML = "Click on the button to get URL"; function functionName(fun) { var val = fun.name; el_down.innerHTML = val; } function GFGFunction() { } function GFG_click() { el_down.innerHTML = window.location.href.split('?')[0]; } </script> </body> </html> Output: Before clicking on the button: After clicking on the button: Example 2: This example replacing the location.search with empty string with the help of replace() method from the location. html <!DOCTYPE HTML> <html> <head> <title> JavaScript method to get the URL without query string </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button onclick = "GFG_click()"> Get URL </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); el_up.innerHTML = "Click on the button to get URL"; function functionName(fun) { var val = fun.name; el_down.innerHTML = val; } function GFGFunction() { } function GFG_click() { el_down.innerHTML = location.toString().replace(location.search, ""); } </script> </body> </html> Output: Before clicking on the button: After clicking on the button: Comment More infoAdvertise with us Next Article JavaScript method to get the URL without query string P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies 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 JavaScript Know the value of GET parameters from URL In this article, we will see how to get the GET parameters from a URL using JavaScript. In order to know the parameters that are passed by the "GET" method, sometimes we need to pass the Email and other details. For that purpose, we are using the following snippet of code. Example 1: This example ge 1 min read How to read a hash with an â&â sign in the URL ? It's a very common situation in web development where users want to extract or read information from any URL. In the case of the unavailability of the server-side code, the programmer can make use of JavaScript or jQuery to read information. To read the URL with an & sign, we can use the split() 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 retrieve GET parameters from JavaScript ? In order to know the parameters, those are passed by the âGETâ method, like sometimes we pass the Email-id, Password, and other details. For that purpose, We are using the following snippet of code. When you visit any website, ever thought about what the question mark '?' is doing in the address bar 2 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 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 access data sent through URL with GET method in PHP ? On a website, we often use forms to collect data, login/signup boxes for users, a search box to search through a web page, etc. So input fields are used to make the website interactive and to collect data. But we know that HTML can not store the data in a database so for that we use a backend script 2 min read How to get the Base URL with PHP ? The term "Base URL" refers to the base or starting point for a set of related URLs. For example, if you have a website with multiple pages or an API with various endpoints, the Base URL is the common part shared by all those URLs. It typically includes the protocol (such as "http" or "https"), the d 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