65

Let's say I want to reload www.domain.com/abc?num=4

But I want to reload www.domain.com/abc ONLY (without everything after the question mark)

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
user847495
  • 9,831
  • 17
  • 45
  • 48

14 Answers14

108
window.location = window.location.href.split("?")[0];
Igor Dymov
  • 16,230
  • 5
  • 50
  • 56
  • 76
    Better: `window.location = window.location.pathname` – Noyo Mar 19 '13 at 21:03
  • 2
    @Noyo actually this answer works better with modern frameworks that use # to denote views and paths ( angular, GWT ). pathname ignores the # – qbert65536 Aug 29 '15 at 17:24
  • 5
    You make a good point, @qbert65536, but this is still better (and avoids function calls): `window.location = window.location.pathname + window.location.hash;` – Noyo Aug 29 '15 at 18:07
  • 4
    It's worth noting that `window.location = window.location.pathname` isn't valid TypeScript syntax. You need to use `window.location.href = window.location.pathname`. – Sean Anderson Jun 06 '19 at 21:03
29

There are a few ways to go about it:

window.location = window.location.href.split("?")[0];

Or, alternatively:

window.location = window.location.pathname;
  • 4
    I don't really understand what it means, but it's worth reading before copy-pasting: https://developer.mozilla.org/en-US/docs/Web/API/URLUtils.pathname – Fabio Iotti Oct 10 '14 at 18:06
20

This is the best and easiest way,

// similar to HTTP redirect
window.location.replace(location.pathname);
M_R_K
  • 5,929
  • 1
  • 39
  • 40
  • In my case, used this for reload the app: const tmpLocation = window.location.protocol + '//' + window.location.host + '/'; window.location.replace(tmpLocation); – Jack Luo Oct 10 '19 at 17:19
  • 2
    Or `location.replace(location.pathname);` for short. – HasanG Feb 10 '20 at 03:13
8

I typically try to avoid making unnecessary function calls whenever I can, especially when the information I need is already provided to me by the DOM. That said, here is probably objectively the best solution:

window.location = window.location.pathname + window.location.hash;

As pointed out in a comment by user qbert65536, there are many popular modern frameworks that use the hash to denote views and paths, which is why using window.location.pathname alone is not enough.

Noyo
  • 4,874
  • 4
  • 39
  • 41
5

Try this Javascript:

location = location.pathname;
Markus Hedlund
  • 23,374
  • 22
  • 80
  • 109
1

I am assuming the user is pressing a button to make this refresh happen. If the button is inside a form element make sure the button type is set to "button" (example below):

<button type='button' id='mybutton'>Button Name</button>

if the type is not set then it will default to type='submit' and will act as a form submit button and thus give you all the extra parameters in the url when reloaded.

Then after that it is a simple javascript refresh call:

window.location.reload();
mark.inman
  • 2,521
  • 2
  • 18
  • 12
  • calling reload triggers a browser refresh which can have the effect of IE complaining about resending a previous form submission. I'd prefer against this for UI/UX concerns. – Richard Barker Oct 20 '15 at 14:55
1

Plain and simple, just tested:

window.location.href = location.pathname;
Matija
  • 17,604
  • 2
  • 48
  • 43
1

Reference

location.search = '';

Or using relative URL, but this will leave the ? in the URL (Reference RFC1808)

<a href="?">
// JavaScript
location = '?';
Community
  • 1
  • 1
Steely Wing
  • 16,239
  • 8
  • 58
  • 54
0
top.location.href = top.location.protocol+top.location.host+top.location.pathname
Paul
  • 139,544
  • 27
  • 275
  • 264
0
document.location = String(document.location).replace(/\?.*$/, '');
Guard
  • 6,816
  • 4
  • 38
  • 58
0

you can use document.URL and split function to the url you want to load and use the method window.location.href to load the page

AmGates
  • 2,127
  • 16
  • 29
0

Try this:

var url = 'www.domain.com/abc?num=4';
alert(url.split('?')[0]);
T. Junghans
  • 11,385
  • 7
  • 52
  • 75
0
var url="www.domain.com/abc?num=4";
window.location.href=url.replace(/^([^\?]+)(\??.*)$/gi,"$1");
Andrew D.
  • 8,130
  • 3
  • 21
  • 23
-1

I don't really understand your question, but maybe this will help you:

<input type="button" value="Reload Page" onClick="window.location.href=www.domain.com/abc">
Akos
  • 1,997
  • 6
  • 27
  • 40