开发者

can window.onPopState() event handler know direction of navigation? (forward,backward)

开发者 https://www.devze.com 2023-04-02 15:24 出处:网络
i have a site that uses ajax navigation with the pushState() method and an onpopstate() handler. when first visiting the main page, i display a default content (without ajax).

i have a site that uses ajax navigation with the pushState() method and an onpopstate() handler.

when first visiting the main page, i display a default content (without ajax).

when someone navigates away from the default page and then goes "back," a null state is supplied when they should be returning to the default page. thus, they stay on the second page of the website instead of the first ( although the handler can easily use ajax to bring the initial content back).

my only way to fix this was to do a pushState on my initial page load with information on what the initial content is. with this method, the "back" button works great!

BUT if someone was at www.stackoverflow.com before going to my site, the goes "back" to stackoverflow and then "forward" to my site again, the initial content is shown, but then reloaded again because the "popped" state is 开发者_如何学Gonot null.

so can i tell, in the onPopState() handler, whether the user is going backward or forward?


I've had some success with using the pageshow event in Firefox to trap that initial entry into the site through forward and back buttons.


can window.onPopState() event handler know direction of navigation? (forward,backward)

There is an issue. You can skip states (hold the back button, select the state you want). This makes it incredibly hard.

when someone navigates away from the default page and then goes "back," a null state is supplied when they should be returning to the default page. thus, they stay on the second page of the website instead of the first ( although the handler can easily use ajax to bring the initial content back).

You can fix that with History.js with it's optional data persistance.

0

精彩评论

暂无评论...
验证码 换一张
取 消