Enabling back/fwd key events for an Ajax Application

本小妞迷上赌 提交于 2019-11-26 20:46:56

问题


I have an application which works heavily on AJAX. However I want to have navigation functionalities in it. To spoof the url, I am changing the location.hash, to generate URL. But if i use back/fwd, only the url changes, but page wont reload. How can I override the hstory.back to reload the page.


回答1:


I don't know of any other way than continuous polling to implement this behaviour. An implementation might look like this:

var lastHash = '';

function pollHash() {
    if(lastHash !== location.hash) {
        lastHash = location.hash;
        // hash has changed, so do stuff:
        alert(lastHash);
    }
}

setInterval(pollHash, 100);



回答2:


You can't exactly capture the back event, but most of these problems have been solved - and a good thing too, it's a hard problem.

Take a look at really simple history (aka RSH) and either implement it or work through it to see how it works.




回答3:


The answer for this question will be more or less the same as my answers for these questions:

  • How to show Ajax requests in URL?
  • How does Gmail handle back/forward in rich JavaScript?

In summary, two projects that you'll probably want to look at which explain the whole hashchange process and using it with ajax are:

  • jQuery History (using hashes to manage your pages state and bind to changes to update your page).

  • jQuery Ajaxy (ajax extension for jQuery History, to allow for complete ajax websites while being completely unobtrusive and gracefully degradable).




回答4:


The balupton answers are really great.

But you also have another jQuery Plugin to handle your ajax requests, it is address.



来源:https://stackoverflow.com/questions/629765/enabling-back-fwd-key-events-for-an-ajax-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!