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 Like