How to save the last url of some "base" url

In a recent application, they wanted to store the last url within a page (or area) the user visited.

This codepen shows how to do it:

If you:

  1. click on Last Dogs Page and then select dog 1,
  2. then click Home Page,
  3. Then if you click Last Dogs Page again, the selection of dog 1 will remain.

It works by listening to when the url changes and checking if the url’s data includes page: "dog". If it does, it saves that url.