Skip to content
Advertisement

Browser go back to active div in previous page

I want to create a list of button where once clicked, it will shows the respective div as below. And in the div, there is a few links that user can click, and once they clicked the link and go back, it will bring them back to the previous div instead of the first default div. Codes as below.

JavaScript

I have Javascript codes that will change the display from none to block when user clicked on the Item.

JavaScript

JavaScript

The problem I have now is that when LinkC is clicked, and user then pressed Back button in the browser, browser goes back to the link of the previous specific div, for example, page.html#m2 but shows div m0. I guess this is because I already set document.getElementById("m0").style.display = "block"; by default so it will always go to div m0 but is there other way to make sure the browser will go back to the previous specific div chosen?

My google finding shows people suggesting history.back() but that means I need to create a Back button but I do not want that. I am sure this is not a complicated issue but I can’t think anymore on how to solve this. Any help is very appreciated. Thank you!

Update: I tried to use history.back() and created a button but this also is not working as it still display div m0. 🙁

Update #2: Tried this on Firefox and it is working though. Is there another way to make sure it can work for different browser?

Advertisement

Answer

Please try the bellow code :

JavaScript

Hope you will be fine with this code.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement