Best Way For Back Button To Leave Form Data

你。 提交于 2019-12-19 03:46:32

问题


I'm creating a web page based on user input from a form. After the user sees the generated page I want to allow them to press the back button and make changes to the form. I would like to display the form as they had filled it out previously. What is the best way to get this behavior (with cross browser support)?


回答1:


After the user sees the generated page I want to allow them to press the back button and make changes to the form. I would like to display the form as they had filled it out previously.

There is no need to add any clever fancy code; that is what browsers will do by default, unless you take active steps to prevent it, such as:

  • breaking the cache with Cache-Control/Pragma headers
  • generating the form page itself from the response to a POST (use POST-Redirect-GET instead)
  • generating the form elements from script

Cookie solutions are fragile and need special handling if you don't want two tabs open at once to get very confused. Make it easy for yourself: let the browser do the work.




回答2:


JQuery has a nice cookie plugin which i used to keep exam data while the user browsed the site for the answers in place.




回答3:


Store the saved information in cookies as delimited data. If the cookie exists, repopulate the form.

If you use document.formName.fieldName syntax, there are no cross-browser issues.

As a fall-back if cookies are disabled, you can store it on the server and do the same with AJAX.



来源:https://stackoverflow.com/questions/507793/best-way-for-back-button-to-leave-form-data

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