what will persist when returning via back button?

后端 未结 4 968
盖世英雄少女心
盖世英雄少女心 2021-01-02 00:49

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

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-02 01:30

    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

提交回复
热议问题