当通过后退按钮返回时,什么会持续? [英] what will persist when returning via back button?

查看:95
本文介绍了当通过后退按钮返回时,什么会持续?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解,当您通过返回按钮返回到某个页面时,复选框将保持选中状态。使用jquery的addClass添加的类但不是。有人可以帮助我理解什么时候,通过后退按钮返回时,什么东西会持续或不是?还有,是否有一种方法来保存变量的值,以便我可以使用变量值来重新创建没有持久的对象?

解决方案

不存在 - 类不会持久化,因为DOM被重新生成,JS变量不会持久因为代码被重新执行等。同时,浏览器实现似乎非常同意保持用户输入的表单数据方便,即使这种行为不是任何官方规范的一部分。



如果你需要一些数据坚持在客户端 - 你有可能在做一些错误的架构。为什么要尝试重写浏览器的后退按钮行为,用户将被恢复到任意状态,而不是去到前一个URL并重新呈现页面?如果您的应用程序交互很重,并且需要这种后退按钮行为,您可能希望回避让用户在独立页面之间导航,以利于发出AJAX请求,并依赖于html5历史记录api ,允许您执行任意任意代码,将网页设置为所需的状态。



您没有详细了解实际预期存储的细节 - 也许是一个更新:

>

我整理了一个小提琴来测试表单数据的持久性当遍历历史记录和隐藏的字段 可以被JS处理时 - 仍然不是一个好主意,虽然imo


I understand that a checkbox will remain checked when you return to a page via the back button. Classes added using jquery's addClass however do not. Can someone help me understand when something will persist or not when returning via the back button? Also, is there a way to save the value of a variable so that I can use the variable values to recreate the objects that did not persist?

解决方案

Nothing persists - classes don't persist because DOM is regenerated, JS variables don't persist because code gets re-executed etc. At the same time, browser implementations seem to pretty much agree to keep user-entered form data for convenience even though this behaviour is not part of any official spec.

If you require some data to persist on the client-side - chances are you're doing something wrong architecturally. Why are you trying to override browser "back" button behaviour for the user to be taken back to an arbitrary state, rather than going to the previous URL and re-rendering the page? If your app is interaction-heavy and this "back" button behaviour is desired, you may want to back away from having the user navigate between independent pages in favour of making AJAX requests and relying on something like html5 history api allowing you to execute any arbitrary code to put the page into a desired state.

You don't go into enough details about what you actually expect to be store - maybe a localStorage or a cookie alternative would be more appropriate

Update:

I've put together a fiddle to test how well form data will persist when traversing history, and hidden fields were available to be processed by JS - still not a good idea though imo

这篇关于当通过后退按钮返回时,什么会持续?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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