Reputation: 10161
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
Reputation: 17597
location.search = '';
Or using relative URL, but this will leave the ?
in the URL
(Reference RFC1808)
<a href="?">
// JavaScript
location = '?';
Upvotes: 1
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
Reputation: 17902
Plain and simple, just tested:
window.location.href = location.pathname;
Upvotes: 2
Reputation: 6350
This is the best and easiest way,
// similar to HTTP redirect
window.location.replace(location.pathname);
Upvotes: 24
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
Reputation: 8230
var url="www.domain.com/abc?num=4";
window.location.href=url.replace(/^([^\?]+)(\??.*)$/gi,"$1");
Upvotes: 0
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
Reputation: 11683
Try this:
var url = 'www.domain.com/abc?num=4';
alert(url.split('?')[0]);
Upvotes: 0
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
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
Reputation: 141839
top.location.href = top.location.protocol+top.location.host+top.location.pathname
Upvotes: 0