Sandip Subedi
Sandip Subedi

Reputation: 1077

Disable only page reload on href onlick on anchor tag

I have a page with Bootstrap nav-bar tabs. When the user clicks on the navbars I want to change the URL (example: localhost:3000/users -> localhost:3000/users?name='test'). I was able to get that done by addding the link on the href but when I click on the tab it reloads the page. I don't want it to reload the page.

I tried preventing default with onclick 'return false;' but it never changes URL on the URL bar.

I know how to solve this with click listener and manually changing the URL on the URL bar. But seems like it should be easier than that.

Is there a way to just disable the page reload but keep the other properties of that link?

Upvotes: 1

Views: 253

Answers (1)

user10279127
user10279127

Reputation:

Try with replaceState, it only changes the current url address without loading it:

window.history.replaceState({}, document.title, "/" + "users?name='test'");

Upvotes: 2

Related Questions