监控 location.hash 是 XHR 应用中历史的解决方案吗?

2023-05-15前端开发问题
6

本文介绍了监控 location.hash 是 XHR 应用中历史的解决方案吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

众所周知,在 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 是一个包含应用程序变量的预定义对象) 这个想法是从哈希值触发应用程序中的每个动作.在体面的浏览器中,哈希值更改会在历史记录中添加一个条目,而在 IE (<= 7) 中则不会.在所有浏览器中,向后或向前导航到具有另一个哈希值的页面不会触发页面刷新.这就是间隔函数接管的地方.每次检测到哈希值更改时(以编程方式,或通过单击后退或前进),应用程序都可以使用该功能采取适当的措施.应用程序可以跟踪它自己的历史记录,我应该能够在应用程序中显示历史记录按钮(尤其是对于 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?

更新:因为我使用自制框架,我不想使用现有框架之一.为了能够在 IE 中使用 location.hash 并将其保存在历史记录中,我创建了一个简单的脚本(是的,它需要一个 iframe),它可能对您有用.我在我的网站发布了它,请随意使用/修改/批评它.

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.

推荐答案

我认为您将很难知道用户是前进还是后退.假设 url 开始/myapp#page1 所以你开始跟踪状态.然后用户做一些事情来制作 url/myapp#page2然后用户做一些事情来再次制作 url/myapp#page1.现在他们的历史是模棱两可的,你不知道要删除什么.

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 来解决您提到的浏览器不一致问题.您只需要在需要它们的浏览器中使用 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.

另一个缺点是用户总是会先选择浏览器的后退按钮,然后再选择您的自定义后退按钮.我感觉每 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)中并不是一直都能完美运行,但它已经被很多用户和开发者使用.他们使用的模式也非常灵活.

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.

这篇关于监控 location.hash 是 XHR 应用中历史的解决方案吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455