Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a back link to a URL that contains an anchor?

Tags:

javascript

I have a page which displays a filter-style menu that users can click to display particular listing category. When they click a listing thumbnail it opens the full listing page. At the top of that page is a "Back" text link. I need that link to return to the exact filtered category of the previous page.

The filtered category URL is something like this:

/residential/#listing-category

On the "Back" text link of the full listing page, I've tried:

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

and also tried:

<a href="javascript:history.go(-1)">Back</a>

However, those only return to /residential instead of /residential/#listing-category

How do I get the Back link to return to the full URL (e.g. /residential/#listing-category)?

like image 489
pixelaid Avatar asked Jan 22 '26 12:01

pixelaid


1 Answers

When using hash links browser does not store the hash part in the history, but you can manually add a new history to the current session whenever moving to a hash link on the page using history.pushState.

history.pushState(state, '', window.location.href) // state can be null or {}
like image 98
Yash Maheshwari Avatar answered Jan 25 '26 04:01

Yash Maheshwari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!