正在监视的历史,在XHR应用程序的location.hash的解决方案? [英] Is monitoring location.hash a solution for history in XHR apps?

查看:146
本文介绍了正在监视的历史,在XHR应用程序的location.hash的解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,在XHR(又名的AJAX)的Web应用程序没有历史记录的程序是建立并点击刷新按钮经常移动的用户他/她的当前活动。我偶然发现的location.hash(如的http://anywhere/index.html#somehashvalue )来规避刷新问题(使用的location.hash,告知您的应用程序它的电流状态,并使用一个页面加载处理程序,以重置状态)。这是非常好的和简单的。

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 stumbled upon location.hash (e.g. http://anywhere/index.html#somehashvalue) to circumvent the refresh problem (use location.hash to inform your app of it's current state and use a page load handler to reset that state). It's really nice and simple.

这给我带来了考虑使用的location.hash跟踪我的应用程序的历史。我不希望使用现有的库,因为他们使用iframe等,因此,这里是我的镍和角钱:当我开始这个应用程序页面加载:

This brought me to thinking about using location.hash to track the history of my app. I don't want to use existing libraries, because they use iframes etc. So here's my nickel and dime: when the application page loads I start this:

setInterval(
       function(){
           if (location.hash !== appCache.currentHash) {
               appCache.currentHash = location.hash;
               appCache.history.push(location.hash);
               /* ... [load state using the hash value] ... */
               return true;
           }
           return false;
       }, 250
 );

(的 appCache 的是包含应用变量predefined对象)的想法是触发从哈希值的应用程序的每一个动作。在体面的浏览器中的散列值的变化添加一个条目的历史,在IE浏览器(小于= 7),它没有。在所有的浏览器,导航后退或前进到另一个哈希值的网页不会触发刷新页面。这也正是intervalled功能接管。随着功能的每次检测到的散列值的变化(以编程方式,或通过单击后退或前进)的应用程序可以采取适当的行动。该应用程序可以保持它自己的历史轨迹,我应该能够present历史按钮,在应用程序(尤其是IE用户)。

(appCache is a predefined object containing application variables) The idea is to trigger every action in the application from the hash value. In decent browsers a hash value change adds an entry to the history, in IE (<= 7) it doesn't. In all browsers, navigating back or forward to a page with another hash value doesn't trigger a page refresh. That's where the intervalled function takes over. With the function everytime the hash value change is detected (programmatically, or by clicking back or forward) the app can take appropriate action. The application can keep track of it's own history and I should be able to present history buttons in the application (especially for IE users).

据我可以告诉这工作的跨浏览器,并有一个在内存或处理器资源方面没有成本。所以我的问题是:这将是一个可行的解决方案来管理的历史XHR-应用程序?有哪些优点和缺点?

As far as I can tell this works cross browser and there's no cost in terms of memory or processor resources. So my question is: would this be a viable solution to manage the history in XHR-apps? What are the pros and cons?

更新:因为我用我的自制软件的框架,我不希望使用现有的框架之一。为了能够使用的location.hash在IE和有它在它的历史上也是如此,我创建了一个简单的脚本(是的,这需要一个iframe),这可能是你有帮助。我出版这在我的网站,随意使用/修改/ critizise它。

Update: because I use my homebrew framework, I didn't want to use one of the existing frameworks. To be able to use location.hash in IE and having it in it's history too, I created a simple script (yes, it's needs an iframe) which may be of use to you. I published it on my site, feel free to use/modify/critizise it.

推荐答案

我觉得你有一个棘手的时间知道,如果一个用户去向前或向后。 说的网址开始/ MyApp的#第1页,所以你开始跟踪状态。 然后,用户就一件让网址/ MyApp的#2页 然后用户做一些事情再次使URL / MyApp的#第1页。现在,他们的历史是不明确的,你会不知道该怎么删除或没有。

I think you'll have a tricky time knowing if a user went forward or back. Say the url starts /myapp#page1 so you start tracking states. Then the user does something to make the url /myapp#page2 Then the user does something to make the url /myapp#page1 again. Now their history is ambiguous and you won't know what to remove or not.

历史的框架使用iframe来解决你所提到的浏览器的不一致性。你只需要使用内置页框在需要它们的浏览器。

The history frameworks use iframes to get around the browser inconsistencies you mentioned. You only need to use iframes in the browsers that need them.

另一个con是,用户总是会去为他们的浏览器后退按钮之前,他们会为您的自定义后退按钮。我有一种感觉阅读史上每250毫秒就会明显过于延迟。也许你可以做的时间间隔更紧密,但我不知道这是否会让事情表现不佳。

Another con is that users will always go for their browsers back button before they will go for your custom back button. I have a feeling the delay on reading the history every 250ms will be noticeable too. Maybe you can do the interval even tighter, but then I don't know if that'll make things perform badly.

我用YUI的历史管理器,虽然它不会将所有的时间很好地工作在所有浏览器(尤其是IE6),它已经使用了大量的用户和开发者。他们使用的模式是pretty的灵活了。

I've used yui's history manager, and although it doesn't work perfectly all the time in all browsers (especially ie6), it's been used by a lot of users and developers. The pattern they use is pretty flexible too.

这篇关于正在监视的历史,在XHR应用程序的location.hash的解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆