Frage

I use window.history.pushState to change the current URL when I open a popup on my page, which works well and is very convenient. Inside the popup I can go to other URLs. The problem is when I close the popup i use pushState again with the first URL, but if press back, it opens the popup again.

What I want is that pressing Back after closing the popup goes back to the previous page before opening the popup.

Home -> Page -> Popup 1 -> Popup 2 -> (Close popup) Page -> (Press back) Home (instead of Popup 2)

War es hilfreich?

Lösung

What I did was using history.go(-x) instead of doing pushState(originalURL).

Home -> (normal navigation) Page -> (pushstate) Popup 1 -> (pushstate + increase popup count) Popup 2 -> (Close popup + go(-2)) Page -> (Press back) Home

EDIT

After doing this i see that most browsers have a limit of the history size (on chrome is 50). So I changed to replaceState when moving from popup 1 to 2.

Home -> (normal navigation) Page -> (pushstate) Popup 1 -> (replacestate) Popup 2 -> (Close popup + go(-1)) Page -> (Press back) Home

This will also close the popup when pressing back.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top