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)
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)
window.location = window.location.href.split("?")[0];
There are a few ways to go about it:
window.location = window.location.href.split("?")[0];
Or, alternatively:
window.location = window.location.pathname;
This is the best and easiest way,
// similar to HTTP redirect
window.location.replace(location.pathname);
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.
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();
Plain and simple, just tested:
window.location.href = location.pathname;
location.search = '';
Or using relative URL, but this will leave the ?
in the URL
(Reference RFC1808)
<a href="?">
// JavaScript
location = '?';
top.location.href = top.location.protocol+top.location.host+top.location.pathname
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
Try this:
var url = 'www.domain.com/abc?num=4';
alert(url.split('?')[0]);
var url="www.domain.com/abc?num=4";
window.location.href=url.replace(/^([^\?]+)(\??.*)$/gi,"$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">