user847495
user847495

Reputation: 10161

How do I reload the page without the query parameters?

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)

Upvotes: 69

Views: 82007

Answers (14)

Steely Wing
Steely Wing

Reputation: 17597

Reference

location.search = '';

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

<a href="?">
// JavaScript
location = '?';

Upvotes: 1

Noyo
Noyo

Reputation: 5134

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.

Upvotes: 10

Matija
Matija

Reputation: 17902

Plain and simple, just tested:

window.location.href = location.pathname;

Upvotes: 2

M_R_K
M_R_K

Reputation: 6350

This is the best and easiest way,

// similar to HTTP redirect
window.location.replace(location.pathname);

Upvotes: 24

mark.inman
mark.inman

Reputation: 2641

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();

Upvotes: 1

Andrew D.
Andrew D.

Reputation: 8230

var url="www.domain.com/abc?num=4";
window.location.href=url.replace(/^([^\?]+)(\??.*)$/gi,"$1");

Upvotes: 0

Akos
Akos

Reputation: 2007

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">

Upvotes: -1

T. Junghans
T. Junghans

Reputation: 11683

Try this:

var url = 'www.domain.com/abc?num=4';
alert(url.split('?')[0]);

Upvotes: 0

AmGates
AmGates

Reputation: 2123

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

Upvotes: 0

Guard
Guard

Reputation: 6955

document.location = String(document.location).replace(/\?.*$/, '');

Upvotes: 0

user193476
user193476

Reputation:

There are a few ways to go about it:

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

Or, alternatively:

window.location = window.location.pathname;

Upvotes: 30

Paul
Paul

Reputation: 141839

top.location.href = top.location.protocol+top.location.host+top.location.pathname

Upvotes: 0

Igor Dymov
Igor Dymov

Reputation: 16460

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

Upvotes: 112

Markus Hedlund
Markus Hedlund

Reputation: 24244

Try this Javascript:

location = location.pathname;

Upvotes: 5

Related Questions