Is monitoring location.hash a solution for history in XHR apps?

前端 未结 3 1200
天涯浪人
天涯浪人 2020-12-05 05:12

As is well known, in XHR (aka AJAX) web applications no history for your app is build and clicking the refresh button often moves the user out of his/her current activity. I

3条回答
  •  心在旅途
    2020-12-05 05:51

    There are 3 issues that tend to get munged together by most solutions:

    1. back button
    2. bookmarkability
    3. refresh button

    The window.location.hash based solutions can solve all three for most cases: the value in the hash maps to a state of the application/webpage, so a user can press one of "back"/"forward"/"refresh" and jump to the state now in the hash. They can also bookmark because the value in the address bar has changed. (Note that a hidden iframe is needed for IE related to the hash not affecting the browser's history).

    I just wanted to note however that an iframe only solution can be used without monitoring window.location.hash for a very effective solution too.

    Google maps is a great example of this. The state captured for each user action is way too large to be placed into window.location.hash (map centroid, search results, satellite vs map view, info windows, etc). So they save state into a form embedded in a hidden iframe. Incidentally this solves the [soft] "refresh" issue too. They solve bookmarkability separately via a "Link to this page" button.

    I just thought it's worthing knowing/separating the problem domains you are thinking about.

提交回复
热议问题