How do I reload the page without the query parameters?

JavascriptHtml

Javascript 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

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

Solution 7 - Javascript

Reference

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser847495View Question on Stackoverflow
Solution 1 - JavascriptIgor DymovView Answer on Stackoverflow
Solution 2 - Javascriptuser193476View Answer on Stackoverflow
Solution 3 - JavascriptM_R_KView Answer on Stackoverflow
Solution 4 - JavascriptNoyoView Answer on Stackoverflow
Solution 5 - JavascriptZnarkusView Answer on Stackoverflow
Solution 6 - Javascriptmark.inmanView Answer on Stackoverflow
Solution 7 - JavascriptSteely WingView Answer on Stackoverflow
Solution 8 - JavascriptPaulView Answer on Stackoverflow
Solution 9 - JavascriptGuardView Answer on Stackoverflow
Solution 10 - JavascriptAmGatesView Answer on Stackoverflow
Solution 11 - JavascriptT. JunghansView Answer on Stackoverflow
Solution 12 - JavascriptAndrew D.View Answer on Stackoverflow
Solution 13 - JavascriptMatijaView Answer on Stackoverflow
Solution 14 - JavascriptAkosView Answer on Stackoverflow