3

how can i remove a querystring of an url when clicking a button? my url looks like this:

www.mysite.com/?tx_felogin_pi1[forgot]=1

this page shows a form for getting the new password, everything is inside an overlaybox. when the user suddenly remembers his userdata again, i need to provide a link in the box to go back to the signin form (also inside an overlaybox), but i can not use a fix url, because the link (and the overlaybox) should be accessable on every page. so i need a link which removes the querystring, independet of the url before.

i tried this, but it won't work:

    function getPathFromUrl(url) {
      return url.split("?")[0];
    }
    // from http://stackoverflow.com/questions/2540969/remove-querystring-from-url

    $("#querystring").click(getPathFromUrl);

the error in my console is: url.split is not a function...

sinini
  • 1,403
  • 3
  • 19
  • 27

4 Answers4

18

What is url? You have to get the url from the window.location object:

$("#querystring").click(function(){
    window.location.href = window.location.href.split('?')[0];
});
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
2

To get the url without querystring use this

function getPathFromUrl() {
  var url = window.location.href; 

  if(url.indexOf("?") != -1)
     url = url.split("?")[0];

  return url;
}

Usage

$("#querystring").click(function(){
     alert(getPathFromUrl());//this will alert the url without querystring
});
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
1

This will remove query string from URL.

Suppose your url is :-

http://localhost:4567/projectName/abc.php?id=307

Just replace the button class with your button's class and run this code for the page which you wants to remove query string.

    $(document).on('click',".button", function(){
    window.location.href = window.location.origin + window.location.pathname;    
    });

After run above code your url will be :- http://localhost:4567/projectName/abc.php

gopal sharma
  • 129
  • 1
  • 6
0
$("#querystring").click(function(){
    // you can use url.split("?")[0]; here, however be sure it's in global scope

});
genesis
  • 50,477
  • 20
  • 96
  • 125