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)?
2
Answers
Here is one of the methods:
Assume the previous page is Page A (URL is:
/residential/#listing-category
) while the destination page is Page B (URL is/destination/
)On Page A, when you click on a link to Page B, record the hash part using either
var hash = window.location.hash.substring(1);
orvar hash = $(location).attr('hash');
(if you have jQuery)Save the
hash
variable in the cookie.Navigate to the Page B.
In Page B, set the "back link" with the Page A URL combined with the cookie
hash
value.Alternatively, you can save the whole URL of Page A in a cookie before you navigate, then set the "back link" with the Page A URL stored in the cookie just now on Page B. The first method is more flexible, though.
Store the URL in session storage