How do I reload the page without the query parameters?
JavascriptHtmlJavascript Problem Overview
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)
Javascript Solutions
Solution 1 - Javascript
window.location = window.location.href.split("?")[0];
Solution 2 - Javascript
There are a few ways to go about it:
window.location = window.location.href.split("?")[0];
Or, alternatively:
window.location = window.location.pathname;
Solution 3 - Javascript
This is the best and easiest way,
// similar to HTTP redirect
window.location.replace(location.pathname);
Solution 4 - Javascript
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.
Solution 5 - Javascript
Try this Javascript:
location = location.pathname;
Solution 6 - Javascript
I am assuming the user is pressing a button to make this refresh happen. If the button is inside a form element
Solution 7 - Javascript
location.search = '';
Or using relative URL, but this will leave the ?
in the URL
(Reference RFC1808)
<a href="?">
// JavaScript
location = '?';
Solution 8 - Javascript
top.location.href = top.location.protocol+top.location.host+top.location.pathname
Solution 9 - Javascript
document.location = String(document.location).replace(/\?.*$/, '');
Solution 10 - Javascript
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
Solution 11 - Javascript
Try this:
var url = 'www.domain.com/abc?num=4';
alert(url.split('?')[0]);
Solution 12 - Javascript
var url="www.domain.com/abc?num=4";
window.location.href=url.replace(/^([^\?]+)(\??.*)$/gi,"$1");
Solution 13 - Javascript
Plain and simple, just tested:
window.location.href = location.pathname;
Solution 14 - Javascript
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">