JavaScript method to get the URL without query string

30 May, 2019
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.
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.
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.
<!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.
<!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:
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Difference Between PUT and PATCH Request
How to append HTML code to a div using JavaScript ?
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?