我有一些 jQuery,它可以在用户执行操作后修改页面上的元素。这些都是持久保存在服务器端的,因此对于用户来说一切看起来都很好且流畅。
当用户单击异地然后单击后退按钮时,除 Opera 之外的所有浏览器都会重新请求该页面,因此更改仍然会显示给用户。然而,使用 Opera,站点会返回到 Jquery 之前的状态(因为它没有从服务器请求页面)。
我可以做一些令人讨厌的事情,比如检查浏览器并刷新 Opera,但显然这并不理想。
有更好的方法来解决这个问题吗?
I have some jQuery which modifies elements on the page after the user performs an action. These are persisted server-side so it all looks nice and fluid for the user.
When the user clicks off-site and then hits the back button all browsers except Opera re-request the page, hence the changes still appear for the user. With Opera however the site returns to it's pre-Jquery state (since it didn't request the page from the server).
I could do something nasty like check the browser and refresh if Opera, but obviously this is not ideal.
Any ideas of a better method to solve this?
发布评论
评论(3)
您是否可以使用一些 HTML5 本地存储来在客户端保留这些更改(对于那些拥有 HTML5 兼容浏览器的用户),并为那些没有兼容的浏览器的用户优雅地降级到服务器请求。
快速浏览了一下后 - 看来 Opera 开始在 Opera 10.5 中支持 LocalStorage(早在 2010 年初),我认为它也会自动更新,所以大多数使用 Opera 的 2.7% 用户应该具有此功能。
我建议使用 http://www.modernizr.com/ 检查功能是否存在,然后也许将结果存储在对象文字 JSON 中,将其结果存储到本地存储中(应该使存储和检索更容易!)
(HTML5 本地存储指南 - http://php-html.net/tutorials/html5-local-storage-guide/)
Could you perhaps use some HTML5 local storage to persist these changes client side (for those users that have HTML5 compliant browsers) and gracefully degrade to server requests for those that don't.
After a quick scoot around - It appears that Opera started supporting LocalStorage in Opera 10.5 (back in early 2010), I think it also auto-updates too, so most of the 2.7% of users using Opera should have this functionality.
I'd suggest using http://www.modernizr.com/ to check for the presence of functionality and then perhaps store the results in a Object Literal JSON the results of that into Local Storage (should make storage and retrieval easier!)
(HTML5 Local Storage guide - http://php-html.net/tutorials/html5-local-storage-guide/)
没有人回答我自己的问题,但似乎以下内容解决了我的问题:
设置这意味着页面将以您离开页面时所处的确切状态加载,这基本上就是我所寻找的。它似乎只是 Opera 的设置,尽管它似乎只是 Opera 的行为不同。
我在此处找到了信息。
Not one to answer my own question but it appears the following solves my problem:
Setting this means that pages will be loaded in the exact state that they were in when you left the page, which is basically what I was looking for. It appears to be an Opera only setting, although it appears to be only Opera which acts differently.
I found the information here.
Opera 有一些启发式方法来确定页面是否需要在历史导航上“重新初始化”。启发式方法包括页面是否正在侦听卸载事件(因为我们无法“撤消”卸载事件所做的任何事情),以及导致导航的代码是否从单击事件运行(因为脚本通常会禁用按钮并使其不易逆转)页面状态的更改)。如果您可以避免触发启发式方法(保持简单),那么即使没有history.navigationMode hack,问题也应该消失。
Opera has some heuristics to determine whether a page needs a "re-init" on history navigation. Heuristics include whether the page is listening for the unload event (because we can't "undo" whatever the unload event did), and whether the code that cause navigation ran from a click event (because scripts often disable buttons and make not easily reversible changes to the page's state). If you can avoid triggering the heuristics (Keep It Simple), the problem should go away even without the history.navigationMode hack.