1

I am trying to write a javascript that detects if the current page contains a "?" in the url and, if it does, get everything in the url up to the "?" and then reload the window with this new url. I've tried this:

var curr_url = window.location.toString();
if (curr_url.indexOf('?')!== -1){
    var goodpart = curr_url.split('?');
    //alert(goodpart[0]);
    window.location = goodpart[0];
} 

But it doesn't seem to work. Either it does nothing for some pages or it works but keeps reloading the page over and over.

Steve
  • 14,401
  • 35
  • 125
  • 230
  • This should work, Is there any URL-rewriting active on serverside? – Dr.Molle Aug 31 '12 at 00:43
  • Agree with Dr.Molle, do you have any meta tags? AJAX? What do you mean it keeps reloading the page? How are you implementing this? Example site? – vol7ron Aug 31 '12 at 00:52
  • The reason it keeps re-loading the page is because when you change window.location, the browser will go and load that page. You keep getting caught in an infinite loop. – Kevin Johnson Aug 31 '12 at 01:01
  • Why is this a duplicate??? – Ethan Sep 04 '17 at 08:50

3 Answers3

1
if (location.search)
{
    location.href = location.protocol + "//" + location.host + location.pathname;
}

That should reload to the current URL minus any querystring.

Collin Grady
  • 2,226
  • 1
  • 14
  • 14
  • You don't need the protocol and the host because they'll default to the current protocol and host anyway. – Neil Jul 11 '13 at 12:11
0
document.location.href=goodpart[0];
Littm
  • 4,923
  • 4
  • 30
  • 38
0
var new_url     = (window.location+'').replace(/\?.*/,'');
window.location = new_url;
vol7ron
  • 40,809
  • 21
  • 119
  • 172