简体   繁体   中英

HTML5 history: how can I see the URL that was popped (ie, the page we were on when we hit the back button)

I am using the HTML5 history API

I notice that if:

  • if I am on a page called /first
  • I use pushState to /second
  • And then hit the back button

The event handler for window.onpopstate() returns the state for /first, as documented.

In order to transition from /second to /first, I would like to see the URL that was popped - ie, /second in this case . What's the best way of doing that?

In lieu of a proper answer, I've implemented the following workaround:

  • Using a data-urlhack attribute on the <body> element when a page is loaded
  • In onpopstate() handler, checking the value of this attribute so I can transition from this page (where the back button was hit) to the page the back button has sent us to.

Also ensure that pages loaded in onpopstate() don't try and push use pushState() themselves.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM