4

I have a page with a searchable table. I have written a "Back" button to go back to the previous page:

<a href="javascript:history.back()">Back</a>

For example, the URL of my homepage is:

https://xxxxxx.com

I enter the page with a searchable table from my homepage (Or any other page):

https://xxxxxx.com/data

When I preform a search, the URL becomes:

https://xxxxxx.com/data?search=*sth*

When I hit the "Back" button, the URL becomes:

https://xxxxxx.com/data

What I actually want is:

https://xxxxxx.com/

(Or the page where the user comes in before /data)


How can I rewrite my javascript on the search button, so that the Back button can get to the actual page?

samhoooo
  • 176
  • 10

1 Answers1

3

Instead of history.back(), which goes back only by one, you could do window.location.replace(window.location.host), which loads the root page.

To get more info on window.location: https://developer.mozilla.org/en-US/docs/Web/API/Window/location

Edit:

If you want to ignore URL parameters, you need to change how you go to another page.

Instead of window.history.pushState(...), you should use window.history.replaceState(...), when the navigation should be ignored on a back click.

Here more to read on that topic: What is the trade off between history push and replace?

MauriceNino
  • 6,214
  • 1
  • 23
  • 60